Logging an Exception to Database

Topics: Exception Handling Application Block, Logging Application Block
Jun 8, 2009 at 9:00 AM

Hi,

I implemented Email Trace Listener and Flat File Trace Listenetr, they are quite well.

But i want to store the exception information in Database.So i configured Database Trace Listener and run the scripts given in EntLib 4.1 which cretaed stored procs and Database tables.Refered all the required dll's including  Microsoft.Practices.EnterpriseLibrary.Logging.Database.

But even if exception occured it is not storing the information in database ,not sending an exception mail and not storing the exception in Flat File.

Please help with this and give me the steps to Log an exception to Database.

Thanks,

Ravi

Jun 8, 2009 at 10:07 AM
Edited Jun 8, 2009 at 10:07 AM

Hi,

Here are the step to log an exception to database:

Assuming that you already have a config and properly referenced required assembly.

  1. Add a Exception Handling Block.
  2. Add a new exception policy.
  3. Select a Exception type.
  4. Add a Logging Handler.
  5. Select a formatter type for your logging handler.
  6. In your logging application block. Add your needed trace listeners where you want to log your exception messages.
  7. Select a Category you want to associate the tracelisteners. Ex. "General".
  8. Add a tracelistener reference from the category selected for each trace listeners needed. Example: For the "General" Category, I will add tracelistener reference for FlatFile trace listener, Email trace listener and Database traceListener.
  9. Going back to the Exception Handling Section, In your logging Handler, Select a value for the "Log Category" property. (This is the category that you configured in step #7 and #8)

 

NOTE: Configuring the database tracelistener would require you to configure a database instance in the DAAB section of the config. Then associated the configured database instance to the "DatabaseInstance" property of the DatabaseTraceListener.

Valiant Dudan
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

Jun 8, 2009 at 10:26 AM

Hi Dudan,

I followed all of these steps and this worked for me to write exception to flat file and email.

But it's not working with Data base logging.I have done what you have mentioned in NOTE also.Please give me anything missing w.r.to Exception handling to database.

the code sample i used is like this.,This code worked for FlatFile Trace Listener and Email Trace Listener.

 

 

 

 

 

 

 

 

 

 

<font size="2">

 

</font>

 

class DALDisplay

{

<font size="2">

 

</font>

 

private const string EXCEPTION_POLICY = "DALPolicy";<font size="2">

 

</font>

 

public static string returnSomeText()

{

<font size="2">

 

</font>

 

string retText="";<font size="2">

 

</font>

 

try

{

<font size="2">

 

</font>

 

int.Parse("Hi");<font size="2">

retText =

</font>

"This Data is originated from DALDisplay Class";

}

<font size="2">

 

</font>

 

catch (Exception Ex)

{

<font size="2">

Ex.Data.Add(

</font>

"DALSection", "This is Exception information from DALDisplay Class"); <font size="2">

 

</font>

 

if (ExceptionPolicy.HandleException(Ex, EXCEPTION_POLICY)) throw;

}

<font size="2">

 

</font>

 

return retText;

Thanks,

Ravi

Jun 8, 2009 at 10:34 AM

Does it throw any exception? or it just not log to the database? can you post your configuration?

Valiant Dudan
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

Jun 8, 2009 at 10:54 AM

It's not throwing any exception and also not logging an exception to data base. <font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="" logWarningsWhenNoCategoriesMatch="true"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add databaseInstanceName="LoggingConnection String" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" formatter="Text Formatter"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Database Trace Listener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font>
<font size="2" color="#0000ff">

 

</font>

add toAddress=Ravi.Yarukala@abc.com  fromAddress=dashdev.notifier@abc.com subjectLineStarter="" subjectLineEnder="" smtpServer="smtprelay.abc.com"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

smtpPort="25" formatter="Email Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"traceOutputOptions="None" filter="Information" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Email TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font>
<font size="2" color="#0000ff">

 

</font>

add fileName="logs\trace.log" header="----------------------------------------"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

footer="----------------------------------------" formatter="Text Formatter"<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="FlatFile TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font>
<font size="2" color="#0000ff">

 

</font>

add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Formatted EventLog TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

formatters> <font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add template="Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

name="Email Formatter" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font>
<font size="2" color="#0000ff">

 

</font>

add template="Timestamp: {timestamp(local)}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

name="Text Formatter" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font>
<font size="2" color="#0000ff">

 

</font>

formatters><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

categorySources><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add switchValue="All" name="CustomCategory"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="Database Trace Listener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="Email TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="FlatFile TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

categorySources><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

specialSources><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

allEvents switchValue="All" name="All Events" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

notProcessed switchValue="All" name="Unprocessed Category" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

errors switchValue="ActivityTracing" name="Logging Errors &amp; Warnings"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="Formatted EventLog TraceListener" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

listeners><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

errors><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

specialSources><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

loggingConfiguration><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandling><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionPolicies><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="BLLPolicy"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException" name="Exception"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add logCategory="CustomCategory" eventId="100" severity="Error" title="Exception Occured in BLL" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="DALPolicy" ><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException" name="Exception" ><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add logCategory="CustomCategory" eventId="100" severity="Error" title="Exception Occured in DAL" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="ExceptionPolicy" ><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None" name="Exception"><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add logCategory="CustomCategory" eventId="100" severity="Error" title="Exception Occured in UI" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

name="Logging Handler" /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font>
<font size="2" color="#0000ff">

 

</font>

exceptionHandlers><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionTypes><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

add><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionPolicies><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font><font size="2" color="#0000ff">

 

</font>

exceptionHandling><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

dataConfiguration defaultDatabase="LoggingConnection String /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

connectionStrings><font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

add name="LoggingConnection String" connectionString="Data Source=DDB09;Initial Catalog=Dash2;User ID=qIntranet;Password=qIntranet "

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

</font></font>
<font size="2" color="#0000ff">

 

</font>

 

providerName="System.Data.SqlClient /><font size="2" color="#0000ff"><font size="2" color="#0000ff">

</

</font></font>
<font size="2" color="#0000ff">

 

</font>

connectionStrings >

Jun 8, 2009 at 11:41 AM

Hi,

your config seems fine. just the same as i did. I think you have a problem accessing your database. I tried stopping my local sql server then try to log to database. It never throw an exception. can you please verify that your connection string is correct and you have access to the database. Also you might take a look at your event log. With my database stopped, saw some logs regarding the failure. The message looks like this

Message: Tracing to LogSource 'General' failed. Processing for other sources will continue. See summary information below for more information. ...............

Valiant Dudan
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

Jun 8, 2009 at 12:01 PM
Edited Jun 8, 2009 at 1:13 PM

I don't have any problem with connection string but i am not using local sql server.I am connceting to different sql server.Is it anything to do with this?

I am not getting any Exception in even event log also.Please let me know what was the reason for it.

If possible give me any sample application.

Thanks,

Ravi

Jun 9, 2009 at 2:49 AM

Try putting the DatabaseTraceListener last on the list of tracelisteners under the CustomCategory.  See if you get logs on your email and file.  Is this an asp.net application?  If it is, make sure it has the necessary permissions on the database you are writing to.

 

Sarah Urmeneta
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

 

Jun 9, 2009 at 5:12 AM

sarah,

"Try putting the DatabaseTraceListener last on the list of tracelisteners under the CustomCategory.  See if you get logs on your email and  file".

I tried this,now i am getting logs in mail and file.

This application is asp.net application.Now i am looking into setting permission on database for my asp.net.

Thanks,

Ravi

Jun 10, 2009 at 3:17 AM

Hi Ravi,

If you need a sample application, kindly send us a mail message so we can send you back the sample app.

Valiant Dudan
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com