Enterprise Library 5.0 Logging Application Block: Log Custome Message using a DatabaseTraceListener

671 Views Asked by At

I am using Enterprise Library 5.0 and My task is to Log information to Database. For this I used Enterprise Library Logging Application Block with Database Trace Listener.

Now, after few days I found that I need some more information to Log into the Logging.LOG Table. They are : User_ID, Session_ID etc.

Per Business requirements, I can't use ExtendedProperty Property of LogEntry class and store this information in the FormattedMessage column of the Log Table in XML fomrat. I want proper Column with Name "User_ID" and "Session_ID" in the Log Table of Logging Database.

Question Is: How can I push customized information in the Log Table?

For this I found something called "CustomTraceListener" where we need to override two methods i.e. Write(string Message) & WriteLog(string Message)

  • How Can I use this CustomeTraceListener to push custome message to Log Table?

  • Write method takes single parameter of type string, means will I get a concatinated string which I need to break into different information and push into database using my own code?

Can someone please send me an implimentation example of CustomeTraceListener to --> Database

Please help me.

Thanks,

Suraj

1

There are 1 best solutions below

0
juFo On

Follow this to create a custom trace listener: http://msdn.microsoft.com/en-us/library/ff647545.aspx

You could then do something like this using the Fluent configuration (code not working but gives you an idea):

    var builder = new ConfigurationSourceBuilder();
    var serviceConfig = new NameValueCollection();
    serviceConfig.Add("Key", "data");
    builder.ConfigureLogging()
        .LogToCategoryNamed("General")
        .WithOptions.SetAsDefaultCategory()
        .SendTo.Custom<ServiceTraceListener>("ServiceTraceListener", serviceConfig)
        .FormatWith(new FormatterBuilder()
        .TextFormatterNamed("Text Formatter")
        .UsingTemplate("Timestamp: {timestamp}...{newline})}"));

    var configSource = new DictionaryConfigurationSource();
    configSource.Add(LoggingSettings.SectionName, builder.Get(LoggingSettings.SectionName));
    var cont = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    return cont.GetInstance<LogWriter>();

(code ± from: Enterprise Library 5.0 Logging using CustomTraceListener and ConfigurationSourceBuilder )

Here is an overview of the data you can use in the .UsingTemplate method: http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.configuration.textformatterdata.template(v=PandP.50).aspx

This template is then being used in your CustomTraceListener TraceData override method (if you follow that Walkthrough from MSDN I linked here).

See also: http://msdn.microsoft.com/en-us/library/ff664363(v=PandP.50).aspx extra formatting: http://msdn.microsoft.com/en-us/library/ff664562(v=PandP.50).aspx