Developer

Log errors easily with the Exception Management Application Block

The Exception Management Application Block (EMAB) can simplify your VB.NET or C# error handling, provided you know how to leverage it properly. This article discusses the potential of EMAB and explains how to realize it.


Error handling (or in more recent geek-speak, exception management) is one of the most common application development tasks, and adding error handing is a critical aspect of building robust applications. But what happens when an error does occur? Naturally, you want to handle it in a graceful fashion. You may also want to record errors to determine trends and prevent them in the future. Logging exceptions has typically meant writing them to a text file, the event log, or a database. But now, Microsoft has released an incredibly flexible way to log your exceptions: the Exception Management Application Block (EMAB).

Features of EMAB
EMAB’s purpose is the automation of exception logging. To facilitate this, EMAB offers a number of features, such as:
  • Single-line exception logging.
  • The ability to turn EMAB on and off without recompiling the application.
  • The ability to log to any source.
  • The ability to change logging features without recompiling the application.
  • Full access to the EMAB source code.

EMAB provides a consistent way to record exceptions in your application. In fact, its whole point is to automate common tasks and help standardize those actions across your enterprise. In addition to EMAB, Microsoft has created a Database Access Application Block, with at least two more Application Blocks currently in beta.

Using EMAB
One of the unusual things about EMAB is that Microsoft ships it as a Visual Studio .NET project in both VB.NET and C# flavors, so you have full access to the source code. (This article will use the VB .NET version.) Microsoft does not ship a compiled version of the code, so you must compile the application as a first step.

After downloading EMAB , run the MSI file to install the Application Block and then open the project. Once in Visual Studio .NET, build the solution. (There are two projects in the solution and you must build both.) If you're using Visual Studio .NET 2003, you'll receive a notice that the project needs to be updated. But EMAB works just fine in VS.NET 2003.

After building the solution, create a new VB.NET Windows application and name it EmabTest. Set a reference to EMAB by right-clicking on the References node in the Solution Explorer window and choosing Add Reference. While in the .NET tab, click the Browse button and navigate to the directory containing the installed DLL. The default installation directory is:
C:\Program Files\Microsoft Application Blocks for .NET\Exception Management\Code\VB\
Microsoft.ApplicationBlocks.ExceptionManagement\bin\
Microsoft.ApplicationBlocks.ExceptionManagement.dll.


Add a button to the form and select it to view the code window. Next, add the following Imports command to the top of the code window:
Imports Microsoft.ApplicationBlocks.ExceptionManagement

Now, add the following code in the Button1_Click sub:
Dim x, y As Integer
Try
x = 5 / y
Catch ex As Exception
ExceptionManager.Publish(ex)
End Try


This code illustrates the simplicity of using EMAB. The code divides five by zero, as VB.NET initializes the variable y to 0 by default. This code generates an OverFlowException, because it uses the standard division operator (/). When the code generates the exception, the catch block runs, and with one line of code, you log the exception to the application log.

Run the application and click the button once. Open up the event viewer and examine the application log. As you can see in Figure A, an entry has been added for the overflow exception from the EmabTest application. The log entry contains a substantial amount of information. Figure A shows the start of the exception information region.

Figure A


Notice two things about the code. First, the publish method of the ExceptionManager object is called without first instantiating the object. The publish method can be called because it is marked as shared in the project. Second, errors are automatically recorded in event logs. This is the default publishing location for EMAB, but you can write custom publishers. Custom publishers allow you to write to a text file, log to a database, send an e-mail, or anything else necessary when an exception occurs.

Configuring EMAB
One of the features that make EMAB so powerful is that you can configure it without recompiling the application. To configure the application, return to VS.NET, right-click on the project, select Add, and then select Add New Item. Scroll down the list and select an application configuration file. Leave it named App.config and click Open.

You have now added an App.config file to your project. This file can contain options that are examined by the application at runtime. By changing values in this file, you can change certain behaviors of EMAB without recompiling the application. The App.config file already has a few lines in it, so modify it to look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.
ExceptionManagerSectionHandler,
Microsoft.ApplicationBlocks.ExceptionManagement" />
</configSections>
<exceptionManagement mode="off" />
</configuration>


Note that the section tag should all be one long line.

Next, run the application and click the button. If you examine the log, the application did not create a new entry. This is because EMAB has been disabled with this line:
<exceptionManagement mode="off" />

You can turn EMAB back on again by switching the line to this:
<exceptionManagement mode="on" />

Turning EMAB on or off in the App.config file is useful, but you can go further. For example, you can specify a particular log name and application name under which your exceptions will be recorded. You modify the App.config by turning exception management back on and then adding a publisher node to the file. The App.config should now look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.
ExceptionManagerSectionHandler,
Microsoft.ApplicationBlocks.ExceptionManagement" />
</configSections>
<exceptionManagement mode="on">
<publisher assembly="Microsoft.ApplicationBlocks.
ExceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.
DefaultPublisher" logname="MyCustomLog" applicationname=
"My EMABTest App" />
</exceptionManagement>
</configuration>


Run the application again and click on the button. You will see a new log named MyCustomLog in the event viewer. In this log is a single entry and the source is My EMAB Test App. You can see this in Figure B.

Figure B


Flexibility and power
Microsoft's EMAB lets you automate and standardize the logging of exceptions from .NET applications. It represents one of Microsoft’s first attempts to provide useful tools for common programming tasks. EMAB allows you to log to anything you want through the use of custom publishers. You can also manipulate a number of EMAB attributes through a configuration file, which makes it easy to change properties without recompiling your application. The combination of flexibility and power makes EMAB a valuable addition to any .NET application.

Editor's Picks

Free Newsletters, In your Inbox