在我们的调试输出到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: