Friday 19 March 2010

Enterprise Library 4.1 – Exception Application Block

 

I am using logging block in order to log all the exceptions.Different Logging blocks can be found at .

Rolling flat file trace listener

Email Trace Listener

Database Trace Listener

 

In this scenario default logging block contains event log trace listener.i.e All the exceptions will be sent to event log which we can find by entering ‘eventvwr’ in start—> Run.

start—> Run –> enter ‘eventvwr’ –> Application:

You can view the errors generated by our application as follows.

image

To do this follow the following steps:

Step 1 :

Add the following references to the web site.i.e Add the following files to the bin folder in your web site.

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll.exclude
Microsoft.Practices.ObjectBuilder2.dll

Step 2 :

Add the code below to the cs file of any aspx page. (Exception generated code.)

       try
       {
           int i = 1;
           int j = 0;
           i = i / j;
       }
       catch (Exception ex)
       {

           //Logger.Write("Our Message : "+ex.Message, "General");
           ExceptionPolicy.HandleException(ex, "MyPolicy");
       }

 

ExceptionPolicy.HandleException will handle the exception and will go to the exception block in web.config to find the place to write the exception.

Logger will write the error message based on logging application block configuration i.e here to the event log.

We can use either of them.

Here we are configuring it to be event logger.

Step 3:

Open web.config file in Enterprise Library Configuration tool and add the exception block and logging application block as..

a) Right click on root of the application –> New –> Exception Handling Application Block

image

b) Now Right Click on the created Exception Application Block –> New –> Exception Policyimage

Rename the Exception Policy to ‘MyPolicy’ – Which we specifiled in our code.

c) Now Right Click on MyPolicy –> New –> Exception Type.

image

Select Exception Type as Exception and click on OK.

image

d) Add the logging application block by right clicking on Exception

image

e) add the Formatter Type and Log Category as follows in logging handler section of the exception application block.

image we are done.

f) Run the application and Check the event log file as above.

    Note :

Logger.Write("Our Message : "+ex.Message, "General");

Here “General” is the Category name in Logging application block. We can add more categories and point each category to a different trace listener like email trace listener , db trace listener etc.


ExceptionPolicy.HandleException(ex, "MyPolicy");

Here “MyPolicy” is the exception policy name in exception application block.We can add more policies and point each policy to a different exception types.

No comments:

Post a Comment