C# .NET - How to do Exception Handling using Enterprise Library and is to be stored in database?

Asked By mohan kumar on 26-Jun-12 11:27 AM
Vikram Singh Saini replied to mohan kumar on 26-Jun-12 11:56 AM
There are lot of articles on internet that could help you for same. I am providing a single link that appears nice to me.


And as far concerned about storing exceptions in database I am trying for it with demo apps.
Neha Garg replied to mohan kumar on 26-Jun-12 12:14 PM
Hello Mohan,

the simple and short example of Exception handling using Enterprise Library.....

ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
Product pro = null;
ProductRepository productRepository = new ProductRepository();
pro = exManager.Process<Order>(() =>  { return productRepository.GetOrder(1000);  }  ,"Data Accessing");

the above method automatically performs exception management and throws the exception based on the configuration

also you can see the complete example given on the below link of logging the Exception Handling:

mohan kumar replied to Vikram Singh Saini on 26-Jun-12 03:28 PM
Hi I need to insert the Error message to sqlserver database. Is there any other method in Enterprise Library to insert the error message in sql . Please give solutions for this.

[)ia6l0 iii replied to mohan kumar on 26-Jun-12 10:03 PM
You need a database TraceListener to be configured at the first place. Following which, configure the logging block to log the messages to the database, similarly the exception handling block to log exceptions to the database. And when an error occurs, use the logging category to log exceptions to the database

There are a good amount of database scripts that you will have to configure to make this work.

As per the config settings, you will have to first add the dataconfiguration section to the ConfigSections node. See below.

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" requirePermission="true"/>

Add a Listener to the logging configuration listeners that can write to the database.
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedData baseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database" databaseInstanceName="LoggingDatabase" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"/>

Add a db policy in the exception policies under Exceptionhandling.
<add name="DB Policy">
<add name="All Exceptions" type="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">
<add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging" logCategory="DBCategory" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority="0"/>

Also add the dataconfiguration tag, which points to the database.
<dataConfiguration defaultDatabase="LoggingDatabase"/>

Note that Loggingdatabase in above node would correspond to a connection string named logging database, like below:
    <!-- Logging Database -->
    <add name="LoggingDatabase" connectionString="Data Source=;Initial Catalog=Logging;Persist Security Info=True;User ID=ejoi;Password=*****;" providerName="System.Data.SqlClient"/>

And in your generic error handler, point to Db policy
ExceptionPolicy.HandleException(ex, "DB Policy");

This should suffice. Thanks

Jitendra Faye replied to mohan kumar on 27-Jun-12 01:04 AM
Enterprise library provides inbuilt support for Error logging and exception handling ,

to handle error using enterprise library you need to include following namespace-

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

This namespace provides all the methods or classes to handle error.

for example follow these links-


Try this and let me know;
RAJASEKHAR RAJENDRAN replied to mohan kumar on 27-Jun-12 03:51 AM
Hi mohan,

There are lot of ways to handle these sort of Exception ..

The below links explains the method to handle the Exception handling using the Enterprise Library that has to be stored in to the database..


Thanks & Regards,