How to Create a Custom Database Trace Listener?

Topics: Building and extending application blocks, Exception Handling Application Block, General discussion, Logging Application Block
Nov 23, 2009 at 11:47 AM
Edited Nov 23, 2009 at 1:56 PM

I have a MyriadException table in database with these fields: MyriadExceptionId, Name, MyriadUsername, Message

I have a custom MyriadException class which inherits from Exception class in code (hope it's not confusing).

I have an exception Policy in the app.config for this:

 <add name="MyriadExceptionPolicy">
        <exceptionTypes>
          <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add logCategory="MyriadExceptionCategorySource" eventId="100" severity="Error" title="Beacon Server Myriad Exception" priority="0" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" name="Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"/>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>

And my Category Source is:

<add name="MyriadExceptionCategorySource" switchValue="Error">
        <listeners>        
          <add name="Database Myriad Exception Listener"/>
        </listeners>
      </add>

I need to create a custom database trace listener so that when I use ExceptionPolicy.HandlePolicy(ex, ExceptionPolicyName.MyriadExceptionPolicy.ToString())), it should log the properties of my MyriadException class in MyriadException table.

(ex would be as type of MyriadException)

How can I achieve this?

Please help.

Nov 24, 2009 at 4:06 AM

Hi,

Is that everything that you will log? Will you log other information to other table?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 24, 2009 at 9:04 AM

I will have other information as well Valiant.

But I want to know how I can create a custom database trace listener and use it from the config file.

Thanks,

 

 

Nov 24, 2009 at 1:46 PM

Hi,

Its just create a new custom trace listener. See this: http://msdn.microsoft.com/en-us/library/cc511727.aspx . But the only difference is that you would implement your own logic of filling up the tables in the database. Here is a sample code snippet:

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class MyCustomDatabaseTraceListener : CustomTraceListener
    {
        public MyCustomDatabaseTraceListener()
        { 
            
        }

        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            base.TraceData(eventCache, source, eventType, id, data);
            if (data != null)
            {
	       //This method "ExecuteStoredProcedure" does the actual
	       //insert of the data to the tables
                ExecuteStoredProcedure(...);
            }
        }

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

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

        private void ExecuteStoredProcedure(LogEntry logEntry)
        {
            using (DbConnection connection = database.CreateConnection())
            {
                connection.Open();
                try
                {
                    using (DbTransaction transaction = connection.BeginTransaction())
                    {
                        try
                        {
                            //actual insert here
                            transaction.Commit();
                        }
                        catch
                        {
                            transaction.Rollback();
                            throw;
                        }
                    }
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
We could also send you a sample solution. Just send us a mail.
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com