The Exception Handling application block is designed to handle most common exception handling tasks in consistent approach. It allows changing exception handling after the application is deployed by using exception policies that are defined in the application configuration file. It offers the best practices for handling exceptions in .NET applications.
This article discusses Exception Handling application block configuration to implement consistent exception handling with in ASP.NET web application and logging the exceptions to windows events log using Logging application block.
The policies define how certain exception types are handled by the Exception Handling Application block. A policy can have multiple exception types associated with it. Each exception type within the policy can have one or more exception handlers.
Step 1 Enterprise Library Configuration Tool
Enterprise Library configuration tool is installed with Enterprise Library to configure application blocks within the application. In order to configure Exception Application block start Enterprise Library configuration tool as shown in picture: 1 below; Right click the Web.config and choose Edit Enterprise Library configuration in context menu.
Note that if the configuration tool is not shown from Web.config context menu, it can be accessed from C:\Enterprise Library 4.1 HOL\Lib [Note that the path may change depending on your installation destination]. Name of the tool is EntLibConfig and the icon is same as the one below in context menu.
When you select Edit Enterprise Library Configuration on Context menu, configuration tool opens Webconfig as shown in below picture: 2 to configure Exception Application block
Step 2 Configuring application to log exceptions
The process of configuring this type of handler involves adding the exception handling application block to the configuration file, creating an exception policy, selecting an exception type, adding the logging handler and finally configuring the logging handler to log exceptions to windows events log as discussed below.
2.1 Add Exception Handling Application block
Using the configuration tool by selecting your application –> New option from context menu, add the Exception Handling Application Block as shown below.
2.2 Adding Exception policy
After creating exception application block, next step is to create an exception policy. Right click the exception handling application block and select New Exception Policy from context menu as shown below.
The new exception policy is named as exception policy by default. You can change the policy to your desired name by selecting Rename from context menu as shown below.
2.3 Adding the Exception Type
Next step in configuration is adding the exception type for this exception policy to handle. To add Exception Type right click the Exception Policy node and choose New Exception Type. This opens a Type selector window as show in below picture, in which you can select a known class that inherits from System.Exception.
Alternatively, you can click the Load an Assembly button to select a custom assembly that contains exception classes. In this example base class Exception is selected.
Step 3 Adding Logging Handler
After choosing the Exception Type, the next step is to configure Logging Application Block to log the exception to desired location. It is feasible to configure Logging Application Block to log the exceptions or other information to desired location such as
- The windows application event log
- An e-mail message
- A database
- A message queue
- A text file
Here Exception Application block using Logging Application block to log exceptions to windows events log.
3.1 Add Logging Handler
Right click Exception from context menu and choose New --> Logging Handler as shown below
Remember that a policy can have multiple exception types associated with it. Each exception type within the policy can have one or more exception handlers.
3.2 Configuring Logging Handler
Here I have explained the logging application block category that will be used to log exceptions. You can configure the Logging Handler by selecting properties window on right hand as shown below.
Note that you can change the category name from General to something meaningful, especially if there will be many types of logging events within your application.
The next step is to choose the Formatter Type. Click the button next to Formatter Type and choose the formatter as shown below.
The output of the TextExceptionFormatter will be simple name-value pairs of data. The output of the XMLExceptionFromatter will contain XML data that is placed in Message section of Log file.
Note that important settings you configure are EventId, Title, LogCategory and Severity as shown below.
That is it!!
You are ready to handle exceptions using best practice in consistent manner and log them using Logging application block.
Step 4 Logging Exceptions to application events
Here I have discussed how the exception is generated and logged to windows application events log using Exception handling policy ‘MyPolicy’ [Note that MyPolicy is the policy name declared in step 2.2 above].
Testing: Logged exception in Windows Application events log
Running the above code logs from ASP.NET Page Load event has logged an event to windows application events as shown below.
Step 5 Adding required Enterprise Library libraries and name spaces
The last part of the configuration is to make sure that you have added below out lined assemblies to your web application \bin folder by selecting Add Reference from context menu and using the required name spaces.
Exception Handling assembly
The core Logging Application Block assembly:-
Formats the exception and sends it to the Logging Application Block:-
- using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
- using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;
- using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
- using Microsoft.Practices.EnterpriseLibrary.Logging;