VS输出窗口(output view)的小技巧--文件行号字符定位

在我们的调试输出到VS输出窗口的信息,有时候我们想要鼠标点击就定位该该文件,改行,甚至该列。在强大的VS工具中已经给我们提供了这个功能,我们只需要把输出到输出窗

口的字符串就是一定的格式化就可以了。c#在VS输出窗口格式为:
文件名称(行号,列号):消息信息。
比如我 test.cs(100,78):消息信息。就是对应我们的test.cs文件的100行78个字符。
在这里我写了一个简单异常输出信息的扩展类。
代码具体如下:   

ExpandedBlockStart.gif 代码
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();           
        }
}

 

图片效果:

2:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值