Overview of Exception Management
When you call ExceptionManager.Publish
, this method does not actually write the exception data to anywhere. ExceptionManager.Publish
calls the classes that are defined in your application's configuration file (Web.Config in ASP.NET or your applications *.config file). Figure 1
shows a graphical decision tree that describes the process the ExceptionManager.Publish
method employs to log errors.
|Figure 1: Exception Management is very flexible through the use of the Config file.|
After it calls the Publish
method the EAAB will check to see if there is an <exceptionManagement> entry in your configuration file. If not, it calls the default publisher to log information about the exception into the machine's Application Event Log.
If an <exceptionManagement> entry exists in your configuration file, EAAB reads this information, invokes the classes identified as exception publishers one by one, and passes the exception information to each one. In this manner you can add new entries to your configuration file, create a new class to handle an exception, and you can thus add publishers at any time!
Example of a Log Entry
When the default publisher of the EAAB logs an error into the log file, it not only records the exception information but it also gathers additional information such as the MachineName, the date and time, the full name of the exception manager that entered the data, the application domain name (in the case of a Web application), the Thread Identity (if any), and the WindowsIdentity of the Windows account the process is running under. Listing 2 shows an example error from a Web application running on my local machine. I purposely misnamed a table in an SQL statement and then attempted to fill a DataReader from that table.
Setting Up Your Config File
To inform the EAAB of your intentions to use different publishers you will enter information into your application's configuration file. To develop an ASP.NET Web application you will enter the <configSections> element shown in Listing 3 into your web.config file. It goes after the <configuration> element but before the <system.web> element.
Next you create the <exceptionManagement> element as you defined in your <section name...> shown in Listing 3. The <exceptionManagement> element will contain one or more <publisher> elements. Each of the different publishers can contain many different attributes. Some of the attributes mean something to the exception management classes or you can add your own custom attributes that you can read from the element once you are within your own custom publisher. Listing 4 shows you three different publishers defined within the <exceptionManagement> element. First you have the Default publisher, next you have a publisher that writes to a SQL Server table, and then you have a publisher that will publish to a specified e-mail address.
Each publisher has a Mode attribute that can have a value of either "on" or "off", allowing you to turn each of the publishers on or off as you need.
Passing Additional Information
The ExceptionManager class allows you to pass a NameValueCollection object as an additional parameter to the Publish method. Inside NameValueCollection you will create name/value pairs. A NameValueCollection object is a keyed-pair collection of values. You can place anything you want into this object.
To help you create Name/Value pairs you might want to create a class with a Shared method into which you pass as many arguments as you wish. This method can create name/value pairs out of each of these arguments. Listing 5 shows an example of calling a class named EMInfo to create these name/value pairs.
The EMInfo Class
The EMInfo class has one method named AdditionalInfo that accepts two parameters: FormName and ApplicationName. The System.Collections.Specialized namespace initializes a new NameValueCollection object and then adds each of the arguments to the collection using the Add method as you can see in Listing 6.
You can add additional arguments to this method by simply adding the new arguments then using the Add method on the NameValueCollection object to insert them into the collection.