- What Is the EMAB, Anyway?
- Logging Exceptions with the EMAB
- Creating a Custom Publisher
- Configuring a Custom Publisher
- Summary
If you decide to publish to something like a remote database or something as simple as a text file, you can create a custom publisher. A custom publisher realizes the IExceptionPublisher interface and implements a single method: Publish.
NOTE
The EMAB also defines an IExceptionXmlPublisher that exists to publish an exception as XML. Instead of an exception, this publisher publishes an XmlDocument containing exception information. I will leave it up to the reader to explore this facet of the EMAB.
The IExceptionPublisher.Publish method accepts an Exception object and two NameValueCollection objects. The Exception object contains the content that you publish. The first NameValueCollection contains additional information captured by the EMAB; the second NameValueCollection contains external optional configuration settings. For example, if you elect to publish to a SQL Server database, you might store the connection string externally. More about configuration settings in a minute.
Instead of convoluting our discussion with information about ADO.NET or file streams, we'll keep our publisher as simple as possible. We'll create a custom publisher that publishes to a dialog created by the MessageBox class. The implementation of the custom publisher is shown in Listing 2.
Listing 2 A custom exception publisher.
using System; using Microsoft.ApplicationBlocks.ExceptionManagement; using System.Collections.Specialized; using System.Windows.Forms; namespace CustomPublisher { public class SimplePublisher : IExceptionPublisher { public void Publish(Exception exception, NameValueCollection additionalInfo, NameValueCollection configSettings) { MessageBox.Show(exception.Message); } } }
When the publisher in Listing 2 is invoked, a dialog window containing the text of the message will be displayed. In order to tell the EMAB to use our publisher we have to add an element to an App.config file. (The App.config applet adds a stubbed out .config file to our project. Select Project, Add New Item and pick Application Configuration applet from the Add New Item dialog shown in Figure 1.)
Figure 1 The Application Configuration applet shown is a file template that adds an XML configuration file to your application.