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
Earn up to 30 extra points for answering this tough question.
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.

http://www.codeproject.com/Articles/10255/Exception-Handling-Logging-Application-Block-Enter

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:


http://imason.com/imason_Blogs/b/suleman_ibrahim/archive/2009/05/25/configuring-enterprise-library-4-0-for-exception-handling-and-logging.aspx
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 . Plz 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.

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

Add a Listener to the logging configuration listeners that can write to the database.
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<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"/>
</listeners>
</loggingConfiguration>


Add a db policy in the exception policies under Exceptionhandling.
<exceptionHandling>
<exceptionPolicies>
<add name="DB Policy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None">
<exceptionHandlers>
<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"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>


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:
<connectionStrings>
    <!-- Logging Database -->
    <add name="LoggingDatabase" connectionString="Data Source=14.23.35.233;Initial Catalog=Logging;Persist Security Info=True;User ID=ejoi;Password=*****;" providerName="System.Data.SqlClient"/>
</connectionStrings>



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-


http://imason.com/imason_Blogs/b/suleman_ibrahim/archive/2009/05/25/configuring-enterprise-library-4-0-for-exception-handling-and-logging.aspx
http://geekswithblogs.net/drewby/archive/2005/01/29/21416.aspx
http://www.programminghelp.com/programming/dotnet/c-microsoft-enterprise-library-exception-handling/
http://msdn.microsoft.com/en-us/library/ff649552.aspx

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..

http://weblogs.asp.net/sukumarraju/archive/2009/10/04/microsoft-enterprise-library-4-1-exception-handling-block.aspx 
http://stackoverflow.com/questions/354764/example-sql-for-logging-exceptions-to-database-using-exception-handling-applicat 


Thanks & Regards,
Rajasekhar.R