6

When Exception happens you can print out the StackTrace and review it.

What if you want to get the StackTrace without an exception happening?

Is there a way to do this?

asked Mar 7, 2009 at 0:12

3 Answers 3

9

When you catch an exception you can construct StackTrace object and extract useful information from it. See the following example:

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

PS: This works even without an exception -- see How to print the current stack trace in .NET without any exception.

answered Mar 7, 2009 at 0:23
Sign up to request clarification or add additional context in comments.

Comments

9

You can print out the stacktrace at any time by calling Environment.StackTrace

string tracktrace = System.Environment.StackTrace;
answered Mar 7, 2009 at 0:13

Comments

1

System.Environment.StackTrace is an awesome tool, but be aware that you don't always get what you're looking for and there are differences between x86 and x64 platforms that can affect the output. Grody details here.

answered Mar 7, 2009 at 1:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.