Logging Block custom Message Formatter

Topics: Logging Application Block
Jul 27, 2011 at 10:43 AM
Edited Jul 27, 2011 at 10:50 AM

I am using a Database Trace Listener for my Website. The problem that i am facing is that inspite of using a Log Message Formatter, i am getting all the details of the Error whereas i want only certain parameters. Here is the code : 

My Log Message Formatter is as : {message}{newline}{timestamp(FixedFormatUSDate)}{newline}{machine}{newline}

but when i run my site and get an Error, my sql profiler catches the foll. Query which is fired : 

 

exec BLogError @eventID=1,@priority=-1,@severity=N'Information',@title=N'',@timestamp=''2011-07-27 09:48:58:627'',@machineName=N'PIREMOVED',@AppDomainName=N'PIREMOVED',@ProcessID=N'4628',@ProcessName=N'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe',@ThreadName=NULL,@Win32ThreadId=N'5168',@message=N'Only this!!!!',@formattedmessage=N'Only this!!!!07/27/2011PIREMOVED'

I dont want the other parameters....only the ones i have specified in the template....any suggestions on what can be done ??

Jul 28, 2011 at 3:32 AM

Hi,

You are using the Exception Logging Handler here right? Note that aside from the text formatter from Logging Block, the Exception Logging Handler also add its own formatter to the message. You need to create your own implementation of TextExceptionFormatter to be able to customize its content. You can refer here on how to create your own Exception Formatter.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 28, 2011 at 10:13 AM

Thanks for the reply but tell me that if i create my own formatter, will i need to create my own tracelistener and customise the procedure which inserts the error details too ?? because then there would be a mismatch of parameters which are supplied to the procedure

Yes, I am using the Exception Logging Handler here.

Jul 28, 2011 at 10:31 AM

Yes, you need to create your own tracelistener too. Sorry I forgot to mention that one.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 28, 2011 at 10:49 AM

Thanks for the quick reply....can you provide me link to a sample ??

Also, tell me how can i configure the logger.write() function to return a value that the log was succesfully written and there were no errors with it ?? 

Currently the code runs and does not inform me whether the logging was succesful or not....anyway i can catch that ??

Jul 28, 2011 at 1:07 PM

Sorry for the confusion earlier. You want to remove the exception details on your logged message right? Then, you'd rather need to create your own implementation of LoggingExceptionHandler (not the ExceptionFormatter, sorry for the mistake). I don't have a link for the sample code but you can take a look at Enterprise Library Source code on how it is implemented. You can find it on the ExceptionHandling solution, specifically on the LoggingExceptionHandler.cs. Then look for the HandleException method:

         public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            WriteToLog(CreateMessage(exception, handlingInstanceId), exception.Data);
            return exception;
        }

modify the WriteToLog method to:

 WriteToLog(string.empty, exception.Data);

so it won't write the exception details. Hope this helps :)

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

 

 

Aug 2, 2011 at 10:31 AM
Edited Aug 2, 2011 at 10:32 AM

i have created my own tracelistener, complete with my own parameters and modified the procedure too. but the problem is that i cannot get the Enterprise library to use the class( I created a separate library and added reference to its dll).. here is how i have added the reference in web.config :

<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.RCSAFormattedDatabaseTraceListener, RCSAELLogger"       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"      databaseInstanceName="RCSA" writeLogStoredProcName="BLogError"      addCategoryStoredProcName="AddCategory" formatter="RCSA_Formatter" />   

can you tell me how to configure EL to use my FormattedDatabaseTraceListener ??

Aug 3, 2011 at 6:35 AM

Hi,

We had a sample custom database trace listener here. Kindly drop us an email so we can send you the solution :)

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us