I was wondering if it's possible to convert the string StackTrace in the exception to a more structured data object?
Or is there a method that can get me this information while I am catching the exception? Maybe something using reflection?
Thanks!
I was wondering if it's possible to convert the string StackTrace in the exception to a more structured data object?
Or is there a method that can get me this information while I am catching the exception? Maybe something using reflection?
Thanks!
Check out the System.Diagnostics.StackTrace class. You can create the object and walk over the frames.
StackTrace st = new StackTrace();
foreach (var frame in st.GetFrames())
{
Console.WriteLine(frame.GetFileName().ToString()
+ ":"
+ frame.GetFileLineNumber().ToString());
}
Essentially, if you want a consistent solution you are out of luck.
You can get a hodge podge solution by storing the stack trace on exception construction.
But, there are no hooks in the framework that are called when an exception is thrown.
Use StackTrace
class with constructor accepting Exception
:
static void ShowExceptionStackTrace(Exception ex)
{
var stackTrace = new StackTrace(ex, true);
foreach (var frame in stackTrace.GetFrames())
Console.WriteLine(frame.GetMethod().Name);
}