开发者

Dumping the call stack programmatically

Looking for a way to programmatically dump the call stack and a .net Win Forms app when ever a section of code is hit. Its something I haven't come across before but will save me some debug time.

Update: Forgot to add, how much overhead would this add to 开发者_StackOverflow中文版the application , i.e. would it slow it down considerably.


System.Environment.StackTrace

Will give you the current stack as a string.

You can also use the StackTrace class as others have pointed out if you have more advanced needs.


You can use:

StackTrace callStack = new StackTrace();

And to then access a specific stack frame:

StackFrame frame = callStack.GetFrame(1);


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

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


Actually it wouldn't slow down your application, because the callstack information mustn't be generated, it's present during the whole processing of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜