how to set custom log entry class variable before calling tracedata

Topics: Logging Application Block
Sep 1, 2012 at 7:29 AM


I need to add 1 more column in the Log table. I am using Enterprise library 5.0 . I created a custom log entry class with variable ProjId. I created a custom trace listener class also. When exception happend am calling the exception handler and pass the parameter as follows : 

 try        {            throw new Exception("test");        }        catch (Exception ex)        {                      bool  rethrow = ExceptionPolicy.HandleException(ex, "DataAccessPolicy");        }


When I call this exception handler it is hitting in the Tracedata method in the LoggingTraceListener. The object am getting is LogEntry. 

Now how can I set the value for CustomLogEntry class variable named "ProjID"  or where can I set the value for this.  The ProjId value will be changed on every exceptions.


I saw many forums saying how to extend the customlog trace listener. But didnt find any where how or where to set the value . Kindly help me as this is urgent delivery for me.

Sep 1, 2012 at 8:07 AM

You will need to create a custom exception handler to create and use your custom LogEntry class.  See the Extended Properties Trace Listener with Custom Exception Handler project on the Samples Page for an example.

If you need to pass additional information into the exception handler you add information to the Data IDictionary property of the Exception class (and then extract the value in the handler).

Randy Levy
Enterprise Library support engineer 

Sep 1, 2012 at 12:28 PM

I added a new customExceptionhandler : IExceptionHandler and inthe 

protected virtual void WriteToLog(string logMessage, IDictionary exceptionData)        {

CustomLogEntry dbLogEntry = new CustomLogEntry();

            dbLogEntry.Message = logMessage;
            dbLogEntry.Severity = System.Diagnostics.TraceEventType.Error;
            dbLogEntry.ProjID = exceptionData["ProjID"].ToString();

            Logger.Write(dbLogEntry);        }

When I add the custom exception handler in the handler part of enterprise library and when I ran the application I got the following error

CustomExceptionHandler, Version=, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.  

Please help me on this.

Sep 1, 2012 at 1:57 PM

You should ensure that the assembly that contains the custom handler (which looks to be CustomExceptionHandler.dll) is deployed in a location where it can be resolved at runtime.  This would usually be in the bin folder or the directory where the application is deployed.  Since your project doesn't have a hard dependency (reference) then you may have to copy the assembly in a post build step.


Randy Levy
Enterprise Library support engineer