46

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
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

3 Answers3

83

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

Cosmin
  • 2,365
  • 2
  • 23
  • 29
larsmoa
  • 12,604
  • 8
  • 62
  • 85
22

Yes ...

StackTrace stackTrace = new StackTrace();           // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames()
ColinE
  • 68,894
  • 15
  • 164
  • 232
15
string stackTrace = Environment.StackTrace;
Phil Gan
  • 2,813
  • 2
  • 29
  • 38