Header/Footer Problem

Topics: User Discussion
May 29, 2007 at 5:19 AM
Hi All,

I need to add specific header and footer to each file instead of each entry. Please help me to where I should insert custom code either to create new custom formatter or to create new TraceListener by extending RollingFlatFileListener or any other interface. I havn't complete grip on design.

Best regards,
ZGulzar
May 30, 2007 at 10:22 AM
Edited May 30, 2007 at 10:22 AM
easiest way to write a new tracelistener is to:
Create a class that derives from CustomTraceListener and specifes an ConfigurationElementType attribute, with its parameter specified as CustomTracelistenerData.

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class MyTraceListener : CustomTraceListener
{
}

In this TraceListener you should then override the TraceData method to perform the actual work. In your case something like opening the textfile, determine at which offset the message needs to be written and write the message to the file.

This tracelistener can then be added to the Enterprise Library configuration using the configuration console, specifying a "Custom Trace Listener" and having the Type property point at your new TraceListener.

If you want this tracelistener to make use of configuration, you can specify a constructor to accept a NameValueCollection in its constructor, that will contain the configuration specified in the configuration console as "attributes", a collection of name/value-pairs that allow you to configure the tracelistener.

HTH
May 30, 2007 at 1:45 PM
Thanks OlafConijn

I have to use Rolling Flat File Listener. If I create new custom trace listener I should take care of all things related to file handling and rolling etc. I think it is very difficult one.

If I call WriteLine(fileHeader) in method RollingFlatFileTraceListener.StreamWriterRollingHelper.PerformRoll(), Is it workable ?

Thanks,
Zeeshan Gulzar


May 31, 2007 at 12:45 AM
Tricky thing here is that it would be hard to put your own logic inside the PerformRoll() method. My guess would be that youd need to copy most of it into your own.

I believe what you could do when modifying this method would be:
1.) Once TraceListener "rolls away" from FileA, write a header to that file.
2.) Before the new Trace is written to FileA, write a header to that file.
( you might have to special-case the first file, in order to get a header in there)

this way you would always lack a footer on the last file. if this is desirable, you could well use the code and structure of the RollingFileTraceListener.

If you always want any file to have a header and footer (at any given time) the structure of the RFTL might not fit your solution. If you want to develop this yourself, it does not have to be complex as the RFTL is, which is optimized around performance and some other issues that might not apply in your specific scenario. In order to have it hosted on the contrib community it should atleast meet some basic expectations, we wouldn't require you to cover all the cornercases the RFTL currently does :-).