Community Pick: Many members of our community have endorsed this article.

Enterprise Library Logging Configuration

Published:
Here is the example of setting up enterprise library configuration in asp.net.

One of the great enhancements that comes with the Enterprise Library is the Enterprise Library Configuration Console. This can be found on your Start menu under Program Files > Microsoft patterns & practices > Enterprise Library. With the console, you can use a fairly user friendly GUI to edit the various configuration files.

Here I am trying to make you understand this feature, by keeping one thing in my mind that “An Image can express thousands words”.

First of all, I download the Enterprise Library and Installed on my computer.

Now open “Enterprise Library Configuration Console” and open Web.Config using its File menu. And this will show your settings here, whatever you have written in you config file.

  Image-1

Now under “Data Access Application Block”, you will find that its must be showing you the connection string you defined in Web.Config file, if you have not created any connection string yet, then you can add this here too.

  Image-2
This is my database, where I have place 3 default tables for handling errors.

  Image-3
And these are stored procedures that are used for the entering data into tables.


  Image-4
So my connection string will look like something like this.

  Image-5
Now here, I add “Exception Handling Application Block”, you may find other lots of other stuff, but we are just concentrating on catching up errors in our database here.

  Image-6
Now under the “Exception Handling Application Block”, I create “Exception Policy”. You can add multiple policies under this block.

  Image-7
Now against each policy I can add “Exception Type”, now it’s up to you what kind of exception type you would like to choose for this policy.

  Image-8
Here we are selecting generalized “Exception” to catch all kinds of errors.

  Image-9
Now we change post handling action to none, this action determines what action should occur after an exception is handled by the configuration exception handling chain. None indicates that no post handling action should occur.

  Image-10
Now we add a logging handler to the system for the same exception we just crated.

  Image-11
And you will see that after adding logging handler it will automatically add “Logging Application Block” to the console.

  Image-12
Now go back to logging handler, and choose the Formatter Type for exception. Formatter Type is basically a format to display the exception.

Image-13
And here we select the text formatter to display the exception.

  Image-14
After selecting the formatter type, we change some another setting as well, like we add some category under Logging Application Block --> Category Source. We can add multiple categories, i.e. Database, Email. And then we choose any category for this handler.

  Image-15
Now we add “Trace Listeners “, We can choose based upon the requirement, in case you need to send error email then choose ‘Email Trace Listener’. Here we are handling exception in database so we choose ‘Database Trace Listener’

  Image-16
Now you will find that ‘Database Trace Listener’ contains some default properties. It automatically has the procedure names to add Category in the database. All categories that you have created and linked will be added to the database automatically.

  Image-17
Now we change its properties, and we select database instance, formatter and Trace Output Options as shown in the below image.

  Image-18
Now under special sources, we have three different sections, 1st is Logging Errors and Warning, 2nd is Unprocessed Category and 3rd is All Events. For all three sections we add Referenced Trace Listener. Also we change its reference to ‘Database Trace Listener’.

  Image-19
This is all what you have to do, here is the references I added to my application. I am not sure all references were added, so please cross check yourself, I hope you can do this at least

  Image-20
And here is my code, where I am trying to raise the error, and please note that I have used the same policy that I created in console.

  Image-21
And this is the output in all 3 tables.

  Image-22
0
6,401 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.