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());
}
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);
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.