Using custom logentry class with exception and logging blocks

Topics: Building and extending application blocks, Exception Handling Application Block, Logging Application Block
Dec 22, 2011 at 9:18 AM

I am using exception block to handle all unhadled exception in my code. (Using ASP.NET 3.5 with Ent. Library 4.1)

In Application error event hadnler of my asp.net global.ascx I write

Exception ex = HttpContext.Current.Server.GetLastError();
ExceptionPolicy.HandleException(ex, "GeneralExceptionPolicy");

In my exception policy I handle all my exception using logging handler.

My logging hadler uses custom trace listner to write to my own structured database.

I have written my custom log entry as

public class DBLogEntry : LogEntry 
{ 
public string UserName { get; set; }
 }

And in my custom trace listener

 

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


        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            var log = data as MyLogEntry;
            if (log != null)
            {
                Write(data);
            }
            else
            {

                base.TraceData(eventCache, source, eventType, id, data);
            }
            
        }

        public override void Write(object o)
        {
            //base.Write(o);

            var log = o as MyLogEntry;

//write all properties of mylog entry into database

        }

In Write or Trace datamethod when I try to cast data to MyLogEntry I get null;

So while genrating eception how do I specify that my exception information should travel to logging handler as MyLogEntry instaed of deault log entry.

Thanks,

Subodh Patil

 

Dec 22, 2011 at 12:29 PM
Edited Dec 24, 2011 at 7:57 AM

Have you created your own custom LoggingExceptionHandler (that implements IExceptionHandler)?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 23, 2011 at 8:25 AM

I tried creating the same.

But then I need to add it as custom handler in my exception policy. How the interaction will happen between custom handler and logging handler?

Also when I keep both the handlers I get following exception on line ExceptionPolicy.HandleException(ex, "PPLGeneralExceptionPolicy");

 "The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, PPLGeneralExceptionPolicy]) failed: Constructor on type 'PPL.Demo.LoggingHandler.PPLoggingExceptionHandler' not found. (Strategy type ConfiguredObjectStrategy, index 2)"

Inner Exception is MIssing MEthodException : {"Constructor on type 'PPL.Demo.LoggingHandler.PPLoggingExceptionHandler' not found."}

And my exception handelr class is pretty simple

[ConfigurationElementType(typeof(CustomHandlerData))]

   public class PPLoggingExceptionHandler : LoggingExceptionHandler, IExceptionHandler

   {

       public PPLoggingExceptionHandler(string logCategory, int eventId, TraceEventType severity,

           string title, int priority, Type formatterType, LogWriter writer)

           : base(logCategory, eventId, severity, title, priority, formatterType, writer)

       {

       }

 

       protected override void WriteToLog(string logMessage, System.Collections.IDictionary exceptionData)

       {

           //base.WriteToLog(logMessage, exceptionData);

           PPLDBLogEntry dbLogEntry = new PPLDBLogEntry();

 

           dbLogEntry.Message = logMessage;

           dbLogEntry.Severity = System.Diagnostics.TraceEventType.Error;

           dbLogEntry.UserName = exceptionData["UserName"].ToString();

           dbLogEntry.Categories.Add("General");

 

           Logger.Write(dbLogEntry);

       }

   }

}

It would be of great help you can share some working example which shows use of exception and logging block with implementation of custom exception handler, custom logentry and custom trace listener together.

 

-- Thanks,

Subodh Patil

Dec 23, 2011 at 10:06 PM
Edited Dec 23, 2011 at 10:12 PM

You could take a look at the Extensibility Hands-on Labs for Microsoft Enterprise Library 5.0 which contains various samples including Exception Handlers.  I know it's version 5 but I think the code should be OK to run (the core interfaces didn't change).  You could also look at the Quickstarts included with the Enterprise Library 4.1 Source Code.  

In terms of the exception you are seeing I think you need to add a constructor to handle NameValueCollection.  Enterprise Library will try to invoke that constructor because the configuration element is CustomHandlerData.

 public PPLoggingExceptionHandler (NameValueCollection settings)
 {
     // retrieve configuration from settings
 }

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com