Create a Wrapper
Wrapper is created to make all our calls routed through one common method.
namespace Infrastructure.ExceptionHandling
{
public static class ExceptionWrapper
{
/// <summary>
/// exceptionHandler
/// </summary>
static IExceptionHandler exceptionHandler;
/// <summary>
/// Gets the exceptionHandler.
/// </summary>
/// <value>The exceptionHandler.</value>
public static IExceptionHandler ExceptionHandler
{
get
{
if (null == exceptionHandler)
{
//Create the Exception Handler
exceptionHandler = ExceptionHandlerHelper.GetExceptionHandler();
}
return exceptionHandler;
}
}
}
}
Create a Helper
Class for Exception
handler. Please note the use of the Exception handler type. This is what corresponds to multiple exception handlers. Currently Microsoft Enterprise Library is used as an example. We can add as many as need be. Also create a Property in the setting file - ExceptionHandlerType
. This Property can be used to switch between the Log Providers. Currently it holds the default value of "ENTLIB
".
namespace Infrastructure.Logging
{
/// <summary>
/// Enumurator for ExceptionHandlerType
/// </summary>
public enum ExceptionHandlerType
{
/// <summary>
/// Microsoft Enterprise Library
/// </summary>
ENTLIB
}
/// <summary>
/// Helper class for Exception Handler
/// </summary>
public class ExceptionHandlerHelper
{
private static ExceptionHandlerType _exceptionHandlerType = ExceptionHandlerType.ENTLIB;
private static IExceptionHandler _exceptionHandler = null;
/// <summary>
/// Initializes the <see cref="ExceptionHandlerHelper"/> class.
/// </summary>
static ExceptionHandlerHelper()
{
try
{
_exceptionHandlerType = (ExceptionHandlerType)Enum.Parse(typeof(ExceptionHandlerType), Properties.Settings.Default.ExceptionHandlerType.Trim().ToUpper());
}
catch //(Exception exception)
{
//Set the default to EntLib
_exceptionHandlerType = ExceptionHandlerType.ENTLIB;
}
}
/// <summary>
/// Gets the Exception Handler.
/// </summary>
/// <returns></returns>
public static IExceptionHandler GetExceptionHandler()
{
switch (_exceptionHandlerType)
{
case ExceptionHandlerType.ENTLIB:
return _exceptionHandler = ExceptionHandlerFactory<EntLibExceptionHandler>.CreateProvider();
default:
return _exceptionHandler = ExceptionHandlerFactory<EntLibExceptionHandler>.CreateProvider();
}
}
}
}
And finally, the implementation of the Entlib Exception handling class. Note the use of the local Exception.
#region Namespaces
using Microsoft.Practices.EnterpriseLibrary.Logging;
#endregion Namespaces
namespace Infrastructure.ExceptionHandling
{
/// <summary>
/// ExceptionHandler class. Uses Microsoft Enterprise Library
/// </summary>
public sealed class EntLibExceptionHandler : IExceptionHandler
{
/// <summary>
/// Handles the specified ex.
/// </summary>
/// <param name="ex">The ex.</param>
/// <param name="policyName">Name of the policy.</param>
/// <returns></returns>
public bool HandleException(System.Exception ex, string policyName)
{
return ExceptionPolicy.HandleException(ex, policyName);
}
}
}}
Don't forget to add the Exception handler configuration in the Application config file
<!-- ENTLIB EXCEPTION HANDLING CONFIGURATION START -->
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
<exceptionHandling>
<exceptionPolicies>
<add name="Default Policy">
<exceptionTypes>
<add name="Exception" type="System.Exception, mscorlib" postHandlingAction="None" >
<exceptionHandlers>
<add name="Infrastructure Exception Handler"
type="Infrastructure.ExceptionHandling.InfrastructureExceptionHandler,Infrastructure"/>
<add name="Logging Block"
logCategory="Error"
eventId="100"
severity="Error"
title="My App"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
priority="0"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<!-- ENTLIB EXCEPTION HANDLING CONFIGURATION END -->
All in place, we need to handle our exceptions using the exception handler that we created. Note that the switch for the exception handler is made at the provider level and not at the consumer level.
//Create the Exception Handler and handle the exception.
ExceptionWrapper.ExceptionHandler.HandleException(ex, "Default Policy");
Note that since, Entlib
uses IExceptionHandler
as one of its interfaces. You would have to rename the IExceptionHandler
with a different name when you try this code. The name used here is only to make it easy to understand.
Also note that, you may have to read the Exception Handling Application Block documentation to understand about the Exception policies supported by the Enterprise Library.
The ground rule of exception handling would still apply: Do not catch an Exception more than once per thread.
References
http://msdn.microsoft.com/en-us/practices/default.aspx
http://www.microsoft.com/downloads/details.aspx?FamilyId=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang;=en
http://www.devx.com/dotnet/Article/31463
Related Articles
Comments
There are no comments yet. Be the first to comment!