Custom RollingFlatFileTraceListener

Topics: Logging Application Block
Dec 15, 2011 at 11:07 AM

Hi mister,

I need generate log file with a concreted format and according products in my application source code:

The pattern for log filename is

{ProductCode} {AnotherCode} YYYYMMddHHmmss.log

Any very good sample with source code for create Custom RollingFlatFileTraceListener for set {ProductCode} and {AnotherCode} values, when it create filename ?? and too, change Header property in code before logfile creates, not in config.

<add name="ErrorsAndWarningsListener"
         formatter="Text Formatter 1"
         header="A PRODUCT"
         timeStampPattern="yyyy-MM-dd HH:mm:ss.fff"
         listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=709072f976b4c05b" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=709072f976b4c05b" />


Dec 16, 2011 at 1:32 AM

Is ProductCode and AnotherCode dynamic or static for the application?  What I mean will the Codes be set at application start up and not change for the duration of the application or will different log writes have different Codes?

Each listener can only write to one log file (this is due to the way file locks are taken out) so you will need a listener for every file you want to create.  If the values are dynamic (not known at design time) then you will probably want to use programmatic configuration of the trace listeners.  In Enterprise Library 5 the fluent configuration makes this quite elegant.

But it looks like you are using Enterprise Library 4.1.  Well, it's not all bad -- you should still be able to use programmtic configuration but it's not as elegant.

Take a look at David Hayden's Enterprise Library 2.0 Logging Application Block Part III - Programmatically Using Logging Application Block With No Configuration File and Alois Kraus` Programatic Configuraton - Enterprise Library (v2.0) Logging Block for good examples on how to do that.

If you go that direction you could create a collection of LogWriters and then return the one that matches your Product or Other code.


Randy Levy
Enterprise Library support engineer