Instrumenting Your Code for Exception Management
I want to dedicate this article to the memory of my great friend Frank Arndt. Frank had a brilliant mind honed by a voracious appetite for the written and spoken word. He was an excellent intellectual sparring partner, and a natural, gifted systems analyst, a Mensch. He will be missed.
Software engineers contrive technical words as easily as lawyers use Latin in legal briefs. Some new words and phrases in the lexicon of professional programmers include instrumentation, application blocks, and agile programming. Like you, I have to fine-tune my etymological filter to maintain a useful signal-to-noise ratio. So, I know that instrumentation includes adding diagnostic code to an application, and that application blocks are mini-frameworks that provide a class of a solution. I still haven't pinned down the boundaries of agile programming, but am in anticipation of a new offering from Dr. Barry Boehm on the subject. (The jury is still out on how much of agile programming is signal and how much is noise, but all such concepts add by varying degrees to the discourse of our profession.)
In this article, I'd like to introduce you to the Exception Management Application Block (EMAB) from Microsoft, which facilitates instrumenting your code for logging exceptions. The signal value of the EMAB is pretty high, as you will see in this article.
What Is the EMAB, Anyway?
The genius of noted speakers and authorsif the glove don't fit, you must acquit; keep it simple stupid (KISS); brevity does not mean inconsequenceis that they are capable of proselytizing a concept into a simple and memorable form. Sometimes alliteration works. Sometimes a phrase is beautifully prosaic and consequently memorable for its luster, and sometimes a clever acronym is employed. Perhaps this suggests that a reasonable qualitative measure of the soundness of an idea is that it is memorableif you catch it, you log it. Clearly there are examples that illustrate this idea. A large number of people remember e=mc2, but of those, how many can suitably explain the general Theory of Relativity?
In keeping with the notion of brevity and consequence, I can tell you that the Exception Management Application Block (EMAB) exists to keep track of exceptions. Simple.
A reasonably .NET-savvy person might quickly retort with "Yeah, but I can do that with the EventLog class. Why do I need the application block?" (A mathematics professor might respond with something about that being a naive response. I won't.)
To create the correct mindset, I want to say to you that good software development is a gradual and progressive layering of expressivity. Good algorithms make me think of Marshal Crenshaw or Dave Matthews playing a solo. A good class might be Yo-Yo-Ma on cello or Stanley Jordan playing an electric guitar as a pianist would, but a great application would visually look like the layering of notes and sounds played by the New York Philharmonic, Boston Symphony, or the Three Tenors (with Aretha Franklin for soul). A great application would be beautiful, elegant, and orchestrated, yet comprised of the same notes available to all musicians.
The EventLog class permits logging to a single location, the .evt log files themselves. Adding a modest and useful layer of complexity, the EMAB permits logging to existing and future repositories, such as the EventLog, in an externally configurable way. Plus, the EMAB supports adding future repositories without rebuilding and redeploying your application. The EMAB abstracts exception logging as a general concept and the EventLog as just one implementation of a publisher.
Now we come down to it. In our business everything is ultimately expressed as lines of code. Let's take a technical look at the implementation of the Exception Management Application Block, and I will demonstrate how to use the EMAB and how to implement and configure exception publishers.