Programmatically get C# Stack Trace

Armen Tsirunyan picture Armen Tsirunyan · Jul 8, 2011 · Viewed 29.2k times · Source

Possible Duplicate:
How to print the current Stack Trace in .NET without any exception?

When an exception is thrown, its text contains the stack trace. Can I somehow obtain the stack trace text(including file and line) without exceptions?

public void f()
{
   //blah
   string stacktrace = ???;
   //blah
}

Answer

larsmoa picture larsmoa · Jul 8, 2011

Environment.StackTrace or System.Diagnostics.StackTrace if you need a more convienient (i.e. not string) representation