Getting Stacktrace

When an Exception occurs, you can print a StackTrace and view it.

What if you want to get StackTrace without exception?

Is there any way to do this?

+6
debugging stack-trace c #
source share
3 answers

You can print stacktrace anytime by calling Environment.StackTrace

string tracktrace = System.Environment.StackTrace; 
+9
source share

When you catch an exception, you can build a 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 exception - see How to print the current stack trace in .NET without any exceptions .

+9
source share

System.Environment.StackTrace is a great tool, but keep in mind that you do not always get what you are looking for, and there are differences between the x86 and x64 platforms that can affect the output. Grody details are here .

+1
source share

All Articles