Custom DB trace listener - Text Formatter ( formattedMessage is not populating correctly)

Topics: Building and extending application blocks, Logging Application Block
Aug 19, 2009 at 11:19 AM


I have created a custom database trace listener by inheriting the custom trace listener. What I have done is I have added 2 new columns to the database log table and I have modified the WriteLog procedure also to add 2 new parameters. I changed the text formatter to include those two properties.

I have created two listeners. One is Event log listener and other is the custom DB listener which uses the text formatter.

My problem is what ever the two columns I have added, I am not getting the formattedMessage column correctly in the database. But in event log the message is cominig correctly.

I have done the following steps

1) Extend LogEntry class and create a new class called "ExtLogEntry" class

2) create a new custom DB trace listener called "ExtDBTraceListener" by inheriting "CustomTraceListener"

3) Override the TraceData and Write(Object o) method and add those 2 new parameters to the stored procedure call like below

<font size="2">



"SourceName", DbType.String, 255, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, log.PackageName);

4) In the config file refer the above custom DB listener and set the formatter as Text Formatter.

5) I have modified the Text Formatter class to include the newly added properties.

<font size="2" color="#0000ff"><font size="2" color="#0000ff">


</font></font><font size="2" color="#0000ff">



add template="Timestamp: {timestamp}&#xD;&#xA;SourceName:{property(SourceName)}

After that I ran the application, I can see the formattedMessage column is not formatted correctly. Both Message and the formattedMessage columns have the same value.

I want the formattedMessage column to have the message formatted.

Could you please let me know what to do to get the correct formatted message in database table?



Aug 19, 2009 at 11:27 AM

When you said you modified the Text Formatter class, di you mean to say that you created your own version of it and used that in the config instead? Or you just modified the Template property in the config editor?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Aug 19, 2009 at 11:31 AM

I just modified the Text Formatter template in the config editor.

Aug 19, 2009 at 11:45 AM
Edited Aug 19, 2009 at 12:14 PM

I see, it doesn't work exactly that way.  The TextFormatter class already has its predefined tokens. If you want to add something without creating your own Formatter class, you could add this token:


wherein userId is a key you added in the LogEntry's ExtendedProperties property.  So if you have you this code:

LogEntry logEntry = new LogEntry();


logEntry.ExtendedProperties.Add("sourceName", "Something");

it will display "Something" in place of that token. 


In addition, you might not have called Formatter.Format on the logEntry's message before passing it to the stored procedure.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Aug 20, 2009 at 5:44 AM

Hi, I did the same thing that what you told, but still it is not working. The message and formattedMessage column both are having the same value in the database table.

I see the following line in the Write method. 

db.AddInParameter(cmd, "formattedmessage", DbType.String, log.Message);

 The log.Message is the actual message. As per the above line it will directly put the message in that column. What if I want to put the formatted message here?


Aug 20, 2009 at 6:04 AM

Please ignore the above reply. I got the answer for the question. I slightly modified the above code as below. It worked. Thanks for your support.

            if (Formatter != null)
                db.AddInParameter(cmd, "formattedmessage", DbType.String, Formatter.Format(log));
                db.AddInParameter(cmd, "formattedmessage", DbType.String, log.Message);