以编程方式转储调用堆栈

寻找一种方法以编程方式转储调用堆栈和.net Win Forms应用程序,当遇到一段代码时。 它之前我没有遇到过但会节省一些调试时间。

更新:忘记添加,这将增加多少开销给应用程序,即它会大大减慢它的速度。

System.Environment.StackTrace 

会给你当前的堆栈作为字符串。

如果您有更高级的需求,也可以像其他人指出的那样使用StackTrace类。

您可以使用:

 StackTrace callStack = new StackTrace(); 

然后访问特定的堆栈框架:

 StackFrame frame = callStack.GetFrame(1); 

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

来自MSDN:

 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()); } 

实际上它不会减慢您的应用程序速度,因为不能生成callstack信息,它在整个代码处理期间存在。