Exception Logging to Database using Enterprise Library 5.0

This tutorial on Exception logging to SQL Server database using Microsoft Enterprise Library 5.0 will get you started in 15 minutes flat. Use it to help yourself whenever you want to configure Exception logging, please find sample application attached.

Follow the steps to configure Exception logging to database by defining Policies for your application using Microsoft Enterprise Library 5.0 (further referred to as EL)

1. Download and Install Enterprise Library 5.0 (further referred to as EL)http://entlib.codeplex.com/releases/view/43135
or from http://www.microsoft.com/en-in/download/details.aspx?id=6836
2. Run the script LoggingDatabase.sql located in your machine at \Documents\EntLib50Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts. Map the path relevant to you system configuration. This will create the required database called "Logging" and the necessary 3Tables and 4Stored Procedures.

3. In your application, Right Click on the web.config and select EL config settings console. When fully configured the settings look like the attached image, please take time to match it with yours.
4. Click on Database settings to expand, then on the + sign to Add Database connection String – enter the connection string of the database you just configured, give it a name, say – "ExcepConnString". In Database provider select SqlDataClient.

5. From the main menu click on block and Add Exception Handling Settings
6. Name the Policy as Policy1 and similarly add one more to call it as Policy2.
7. In the Handlers, enter Policy1 and Policy2 in the Title section respectively so that you will be able to track in db for which policy the exception corresponds.
Please be aware that for each Exception Type you should have handlers defined.

8. From the main menu again add Logging settings block.
9. In Logging Target Listener, click the + sign to add Database Trace Listener.
10. In database instance select the connection string name as defined above from the dropdown.
11. In select formatter as Text Formatter.

12. In Categories Section of Logging settings block – For General Tab, add Listeners name and select Database Trace Listener from the dropdown.

13. Click Save and you are done configuring the settings for web.config. If you get error in configuring this, I will not be of any help to you, better to exit without saving and start all over again!

Configuring the way as we have done here lets you log exceptions depending upon the exception types and if you have multi-tier architecture in your application and you want different exception category and severity for each layer by defining various policies according to layers. This helps to drill down as to wherefrom the exception originated and it becomes easy to diagnose.

Now add references to your project, browse to reach - C:\Program Files\Microsoft Enterprise Library 5.0\Bin and choose the relevant dll's, for if you rae using for database choose all that have data in their name and for caching choose caching dll's.

Add namespace to your codebehind:

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;

And the code that will create the exception:

protected void Page_Load(object sender, EventArgs e)

public void throwex()
int i = 5;
int j = 0;
int z = i / j;
catch (Exception ex)
bool rethrow = ExceptionPolicy.HandleException(ex, "Policy1");
//if (rethrow)
// throw;
//Code ---

1. If it doesn't work, do not give up. EL's config interface is very confusing and it may ask you for 1000 tries before actually letting you log the exceptions!
2. Check for the connection string, in my case I found that the password was not getting copied from the server explorer and I never had a chance to look back – If I copy, I expect the same to be pasted – not a part of it!! So for me this was the issue and it was difficult to track as you get no error for this.
3. Also, I found that for each Exception Type you should have handlers defined, do not forget this.

Sample application attached, configure the database, change the connection string and start using it.

Feel free to ask if you encounter any problem.



  • Sample application for exception logging to database using policy (44107-75845-Sample-application-exception-logging-database-using-policy.7z)
  • Database script for exception logging to database using policy (44107-75745-Database-script-exception-logging-database-using-policy.txt)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: