Using DataAccess Trace Listener for Logging Application Block

Topics: Logging Application Block
Apr 15, 2011 at 10:24 AM


I want to use the logging Application Block for logging in my .NET web application but I want to use DataAccess TraceListener whch will trace the logging to a Mysql Table audit_log.

I am very new to this so I dont know how to proceed. I also went through HandsOn Lab for Enterprise Library but no help. Could some giude me please? I need to do it very quickly.

Thanks in advance.

Apr 15, 2011 at 11:21 AM


Basically, you need to create a custom database listener that handles logging to your MySql Database. Here is a sample code found on this thread

using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

namespace ApplicationBlock_LoggingDrill_Basic
    public class CustomDBTraceListener : CustomTraceListener

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
                  if (this.Filter == null || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
                if (data is LogEntry && data != null)

                    Database db = DatabaseFactory.CreateDatabase(this.Attributes["Database"]);

                    DbCommand cmd = db.GetStoredProcCommand(this.Attributes["WriteStoredProc"]);

                    LogEntry le = (LogEntry)data;
                    db.AddParameter(cmd, "severity", DbType.String, 32, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, le.Severity.ToString());
                    db.AddInParameter(cmd, "timestamp", DbType.DateTime, le.TimeStamp);
                    db.AddParameter(cmd, "message", DbType.String, 1500, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, le.Message);
                    db.AddInParameter(cmd, "ExtendedProperty", DbType.String, System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                    db.AddOutParameter(cmd, "LogId", DbType.Int32, 4);

        public override void Write(string message)
            throw new NotImplementedException();

        public override void WriteLine(string message)
            throw new NotImplementedException();

And your configuration will be..

<add Database="CustomLogging" WriteStoredProc="CustomWriteToCustomDB"
        Param1="Value1" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="Warning" type="ApplicationBlock_LoggingDrill_Basic.CustomDBTraceListener, ApplicationBlock_LoggingDrill_Basic, Version=, Culture=neutral, PublicKeyToken=null"
        name="CustomDBTraceListener" initializeData="" formatter="CustomDBMessageFormat" />
Apr 20, 2011 at 10:24 AM


Thanks for your response.

I am able to make it work.

Apr 20, 2011 at 1:34 PM


According to new requirements, I need to implement Exception Handling block using the same Logging block for logging the exceptions.

Could you please help me out in this also.


It would be a great favor.

Apr 24, 2011 at 11:10 AM


Glad to hear that you’re able to make your Custom Database Logging to work J. Since you already have accomplish this then to utilize this in your Exception Handling block what you need to do is to configure EHAB using a Logging Handler in your configuration. Taking the following steps from the documentation here are the following;

 To configure the application exception policies

  1. Use the configuration tools to create an exception handling policy for your application layer. For more information, see Entering Configuration Information.
  2. Specify the exception types to be processed and logged.
  3. Add the logging handler as the first handler for each exception type.
  4. Configure the logging handler:
    1. Enter the event ID.
    2. Enter the logging categories. > Make sure that the Log Category configured here is the one you have used from you logging. In your case this probably be the “CustomLogging” if this is the one intended for use.
    3. Select the severity.
    4. Enter the title of the log entry.
    5. Select the formatter type name.
    6. Enter the priority.
  5. Add any additional exception handlers to be invoked after the logging handler.

Your Configuration may look something like this once done with the configuration.

      <add name="Logging Policy">
          <add type="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="None" name="Exception">
              <add logCategory="CustomLogging" eventId="100" severity="Error" title="Logging to Custom DB"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />

Then in your application code

  // Run code.
catch(Exception ex)
  bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");
  if (rethrow)

Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.