zoukankan      html  css  js  c++  java
  • VS输出窗口(output view)的小技巧文件行号字符定位

    在我们的调试输出到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();           
            }
    }

    图片效果:

    2:


    作者:破  狼
    出处:http://www.cnblogs.com/whitewolf/
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼

  • 相关阅读:
    Apache 基础
    Oracle
    DevOps
    Ansible
    Iptables 和Firewall 防火墙
    防火墙
    Linux
    Intellij IDEA 快捷键大全
    Java8,9 新特性
    Jmeter
  • 原文地址:https://www.cnblogs.com/whitewolf/p/1875473.html
Copyright © 2011-2022 走看看