代码之家  ›  专栏  ›  技术社区  ›  RC1140

以编程方式转储调用堆栈

  •  30
  • RC1140  · 技术社区  · 17 年前

    正在寻找一种方法,以编程方式在遇到某段代码时转储调用堆栈和.net Win Forms应用程序。这是我以前没有遇到过的,但可以节省一些调试时间。

    4 回复  |  直到 9 年前
        1
  •  70
  •   Ryan Cook    17 年前
    System.Environment.StackTrace
    

    将以字符串形式提供当前堆栈。

    您也可以使用 StackTrace 如果您有更高级的需求,请按照其他人指出的那样上课。

        2
  •  15
  •   Wim    17 年前

    StackTrace callStack = new StackTrace();

    然后访问特定的堆栈帧:

    StackFrame frame = callStack.GetFrame(1);
        3
  •  1
  •   Arthur    17 年前

    http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

    using System.Diagnostics;
    
            StackTrace st = new StackTrace(true);
            for(int i =0; i< st.FrameCount; i++ )
            {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}",
                    sf.GetMethod());
    
                Console.WriteLine("High up the call stack, Line Number: {0}",
                    sf.GetFileLineNumber());
            }
    
        4
  •  -3
  •   Tomas Walek    17 年前

    实际上,它不会降低应用程序的速度,因为调用堆栈信息不能生成,它在代码的整个处理过程中都存在。