VS输出窗口(output view)的小技巧--文件行号字符定位
2010-11-12 10:57 破狼 阅读(5538) 评论(5) 编辑 收藏 举报在我们的调试输出到VS输出窗口的信息,有时候我们想要鼠标点击就定位该该文件,改行,甚至该列。在强大的VS工具中已经给我们提供了这个功能,我们只需要把输出到输出窗
口的字符串就是一定的格式化就可以了。c#在VS输出窗口格式为:
文件名称(行号,列号):消息信息。
比如我 test.cs(100,78):消息信息。就是对应我们的test.cs文件的100行78个字符。
在这里我写了一个简单异常输出信息的扩展类。
代码具体如下:

public class OutPutExceptionEx
{
public static void WriteLine(string message, Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame frm = st.GetFrame(0);
if (frm != null)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}({1},{2}):{3})", frm.GetFileName(), frm.GetFileLineNumber(), frm.GetFileColumnNumber(),
message));
}
}
public static void WriteLine(Exception ex)
{
WriteLine(ex.Message, ex);
}
public static void Write(string message, Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame frm = st.GetFrame(0);
if (frm != null)
{
System.Diagnostics.Debug.Write(string.Format("{0}({1},{2}):{3})", frm.GetFileName(), frm.GetFileLineNumber(), frm.GetFileColumnNumber(),
message));
}
}
public static void Write(Exception ex)
{
Write(ex.Message,ex);
}
}
//测试
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception("这个发生了一个错误!");
}
catch (Exception ex)
{
OutPutExceptionEx.Write(ex);
}
Console.WriteLine("ok");
Console.Read();
}
}
{
public static void WriteLine(string message, Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame frm = st.GetFrame(0);
if (frm != null)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0}({1},{2}):{3})", frm.GetFileName(), frm.GetFileLineNumber(), frm.GetFileColumnNumber(),
message));
}
}
public static void WriteLine(Exception ex)
{
WriteLine(ex.Message, ex);
}
public static void Write(string message, Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame frm = st.GetFrame(0);
if (frm != null)
{
System.Diagnostics.Debug.Write(string.Format("{0}({1},{2}):{3})", frm.GetFileName(), frm.GetFileLineNumber(), frm.GetFileColumnNumber(),
message));
}
}
public static void Write(Exception ex)
{
Write(ex.Message,ex);
}
}
//测试
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception("这个发生了一个错误!");
}
catch (Exception ex)
{
OutPutExceptionEx.Write(ex);
}
Console.WriteLine("ok");
Console.Read();
}
}
图片效果:
2:
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架