Logging Stack Traces
From a debugging developer’s point of view, the most precious treasure to tackle a problem is most likely a stack trace. Having this information available answers the question about what the code is doing at a certain point in time. This piece of information is readily available in a debugger, but it’s also sometimes useful to log a stack trace in code.
Here the StackTrace class in System.Diagnostics comes up as the hero of the day. The creation of a new instance of this class captures the stack trace of the current thread. A few constructor overloads exist to extract a stack trace from an Exception object, to skip a specified number of frames, and so forth. A silly example is shown here:
static void Bar(){ var trace = ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access