Logging an exception using Logging handler

Logging exceptions/errors provides valuable information; this information can be analyzed and issues can be resolved proactively. The Logging handler leverages the Logging block to log exception information. As mentioned earlier, every exception type can have one or more handlers and generally the Logging handler is used in combination with a Wrap or Replace handler.

Configuring a Logging handler

We will update the existing configuration and add a Logging handler for the DbException type associated to the policy named Data Access Layer Policy.

Right-click on the DbException type in the Exception Types section of the Data Access Layer Policy and click on Add Handlers | Add Logging Exception Handler as shown in the following screenshot:

Configuring a Logging handler

The following screenshot shows the default configuration of Logging Exception Handler:

Configuring a Logging handler

Tip

Notice the Logging Exception Handler is moved up and is the first handler in the hierarchy; this is explicitly done to log the original exception and not the replaced or wrapped exception.

The following screenshot shows the Logging Settings added to the configuration editor.

Configuring a Logging handler

Note

The Logging Application Block is explained in more detail in the respective chapter. For this particular functionality, we will be using the default logging configuration.

Now, in our code when we encounter an exception of type DbException either managed through the Process or HandleException method of the ExceptionManager class, the application block will first log the exception in Windows Application Event Log as it is the default configuration. Later, the second handler will be invoked, which replaces the original exception with a new exception.

The following screenshot shows the exception logged in Windows Event Log; this is the execution result of Exception Logging Demo provided as part of the sample application with this book:

Configuring a Logging handler
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.28