EntLib 5.0 Logging Exception Handler

Topics: Logging Application Block, Policy Injection Application Block
Mar 15, 2010 at 7:18 PM
Edited Mar 15, 2010 at 7:19 PM

I am using EntLib5.0 Beta1. I have created an Exception Handler and configured it to log to the database. I am able to configure the TextFormatter the Database Trace Listener uses. I am wondering if it is possible to configure the Formatter that the Logging Exception Handler uses?

Specifically what I've done is change the Database Trace Listener formatter's timestamp to use local & formatted time. {timestamp(local:yyyy-MM-dd HH:mm:ss.fff)}

What I would like to do is change the Logging Exception Handler's Formatter to output the time in local time if possible. There are currently 2 time stamps in the message. 1 is hard coded to LocalTime and the other is hard coded to Utc. I would like to use the app.config to make both of those times Local.

Thanks

Mar 16, 2010 at 7:14 AM

Hi,

Let me just clarify the question. First, You are using Exception Handling Application Block here, right? You said that you've used the token {timestamp(local:yyyy-MM-dd HH:mm:ss.fff)}, so it changes the timestamp to the local time in the FormattedMessage column. Are you trying to change the timestamp to local of the Message column?

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

Mar 16, 2010 at 6:38 PM
Edited Mar 16, 2010 at 7:28 PM

You are correct.

Yes I am using Exception Handling App Block. You are correct I have used the timestamp token to change the timestamp to local time. This results in changing the FormattedMessage. To do this I modified the Database Trace Listener's TextFormatter. This was / is very straight forward.

I will want to change the text formatter associated with Logging Exception Handler to modify the Message. That is my question, how do I modify the Logging Exception Handler's text formatter? There doesn't seem any obvious way in the Ent Lib Config App Block Console or in the xml itself.

 

Thanks

Beezler

Mar 17, 2010 at 4:36 AM
Edited Mar 17, 2010 at 4:37 AM

Hi,

Modifying the text formatter would not format the Message, since you are using exception handling application block, the one who's responsible for formatting your message is the exception formatter. I would suggest you try to implement a custom exception formatter. Implement the methods that writes the dates (So far I've identified WriteDateTime and WriteAdditionalInfo methods), you can now change them to the local date time. This works well in version 4.1, please try in 5.0.

Here is a sample:

public class CustomExceptionFormatter : TextExceptionFormatter
    {
        public CustomExceptionFormatter(TextWriter writer, Exception exception, Guid handlingInstanceID)
            : base(writer, exception, handlingInstanceID)
        {
        }

        protected override void WriteDateTime(DateTime utcNow)
        {
            base.WriteDateTime(utcNow.ToLocalTime());
        }

        protected override void WriteAdditionalInfo(System.Collections.Specialized.NameValueCollection additionalInformation)
        {
            if (additionalInformation.AllKeys.Contains("TimeStamp"))
            {
                additionalInformation["TimeStamp"] =
                    Convert.ToDateTime(additionalInformation["TimeStamp"]).ToLocalTime().ToString();
            }
            base.WriteAdditionalInfo(additionalInformation);
        }
    }

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