Menu

Reading .NET Exceptions from the Event Viewer

February 4, 2014 - .NET, C#, Programming

Handling and dealing with Errors can be tricky. When your program crashes you want to fix it as soon as possible. One of the most valuable pieces of information- when the Error is generated from a .NET Program- is the Exception type as well as the stack trace, which can be used to try to determine the cause of the problem.

More prudently, of course, it makes sense to also log data. My personal approach is to implement a relatively simple class that will write log files to the Application Data folder, and overwrite old logs. It is implemented as a TraceListener, and merely needs to be invoked to start capturing Debug output to a file. The advantage here is that the Debug statements are removed in a release build; the downside is that the Debug statements are removed from a release build. Using the Trace class instead of the Debug class for writing debug output will allow that trace output to be captured by the “DebugLog” class, listed below.

The debug logs captured can be exceedingly useful- a more full implementation of a Debug handler, or unhandled exception handler, could very well zip up some of those files and E-mail them to an appropriate Support address.

But what about Exceptions that occur that you don’t catch? Sometimes you simply don’t catch all your exceptions- and it’s reasonable, having a catch-all Exception handler can cause just as many unexpected issues as catching only those exceptions you know how to handle. Thankfully, all .NET Exceptions are actually logged to the Event Viewer.

A Application that was simply too exceptional for this world... hyuk hyuk.

A Application that was simply too exceptional for this world… hyuk hyuk.

The information is therefore available; but guiding a client or customer through the process of wandering through the Event Log is a bit much. So it makes perfect sense to let an Application do this job for you. Thankfully, .NET has a rich capability in terms of inspecting the Event Log. I Created a quick Application to demonstrate (Includes source code).

The principles exercised in the project could easily be used to create a zip or other archive filled with useful diagnostic information in the case of an unexpected crash; that zip could even be automatically E-mailed to support (as I mentioned above). With such a wealth of information it can help eliminate a lot of time-wasting back-and-forth trying to determine the cause of a problem. If nothing else, something like this Application would make finding the proper information slightly easier (with the appropriate tweaks to make the Stack Trace visible).

Have something to say about this post? Comment!