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 = ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.