Exception Logging with EntLib Made Simple
Logging application exceptions is like getting a new muffler. It isn’t exciting. It isn’t very fulfilling. It just costs time and money.
Also, it is something that needs to be done.
So, can we make exception logging as frictionless as possible?
- Roll your own logging library.
- Repeat the same code a lot in your application calling your library.
Kidding. On both fronts.
Since the problem is just about logging exceptions, adding an extension method to the basic Exception class makes a lot of sense.
Now, using this extension method, logging an exception looks like this:
Enterprise Library Logging Application Block
Enterprise Library makes what happens inside the extension method an easy implementation, too. The act of logging is simple enough with EntLib, but the real value is in how the configuration story lets an application administrator decide how the logging solution will actually behave at runtime.
For instance, when building a recent application, I asked the client if they wanted to receive email when a significant application error occurred. Their response was, “Just write to the Windows Event log. We have a monitoring application that will email us.”
Fine by me! Windows Event log, text file, email, it’s all the same because the ultimate destination of the log message is a configuration setting.
Anyway, the implementation of the extension method can look as simple as this.
Of course, I ended up with some overloads and other niceties , but the basic approach remains sound.