Team LiB
Previous Section Next Section

Using the Exception Management Block

Now you have a couple of new DLLs. If you are paying attention and looking through the code, you will notice that you have a new user control called "exceptionManagementEventLogInstaller."

This set of classes provides a great deal of functionality for just a single line of code on your part. For instance, if you publish an error you will get the following information written to your log file:

The following are several benefits to using this set of classes:

In their basic configuration, the exception classes publish errors in the Windows event log. I covered the event log back in Chapter 7. Of course, you can create your own publishers and redirect error-capturing text to any output you like.

As I mentioned in Chapter 7, it is best if you do not clog up the Windows event log file with all kinds of stuff. Besides this being wasteful, it makes it difficult to distinguish your log entries from entries of any other program running on your computer. You are better off defining your own publisher. The help file shows you how to do this easily enough. After reading this book, you certainly have enough knowledge to do so without too much trouble.

So, once you have included references to the new DLLs you created, you need just one line of code to set the exception ball rolling. Here it is—don't blink.

C#

catch(Exception e)
{
  ExceptionManager.Publish(e);
}

VB

Catch ex as Exception
  ExceptionManager.Publish(e);

That's it. Where it goes is defined by values in your <application name>.exe.config file. What gets published is defined by the default publisher or by you if you create a new publisher.

By the way, in keeping with the spirit of XML, you're able to publish exceptions in XML format as well. Pretty cool, isn't it?


Team LiB
Previous Section Next Section