Multiple formatting options?

Topics: Logging Application Block
Jan 26, 2007 at 1:00 AM
I was wondering if it is possible to have multiple formatters using a single FlatFileTraceListender.

For example, we want to log to a single file. When there is an error, we capture a complete dump of information (for example, all of the information shown in the default formatter).

Other times we just want to log a single line of information, plus the date/time, that indicates what the program is doing, which helps our debugging.

Do you have any suggestions?
Jan 26, 2007 at 2:35 PM
There is a 1:1 relationship between a TraceListener and a Formatter, so I don't know how you would specify multiple formats. You also don't have access to programmatically change the formatter at runtime.

You could look into using the Logging Application Block programmatically as I talk about in the following tutorial:

Programmatically Using the Logging Application Block

and see if that helps.

You may be able to use the ExtendedProperties to pull something off like that, too, but it may end up being a bit kludgy and hard to maintain.




David Hayden
Microsoft MVP C#
Jan 26, 2007 at 2:38 PM
Actually, to be more accurate, you can assign the same Formatter to many TraceListeners in the configuration, but a TraceListener can currently only have 1 Formatter.




David Hayden
Microsoft MVP C#
Jan 26, 2007 at 7:51 PM
Dave is correct here. However you should be able to create two different Flat File TraceListener instances that both point to the same file, configure a different formatter for each, and reference them from different category sources.

The only issue I can see is that the Flat File Listener will keep a lock on the file, so you probably won't be able to get them both pointing to the same file unless you modify the locking behavior. The new v3 rolling listener includes a new LockBehavior property that lets you configure whether the file should be kept open, or opened and closed for each write - so this should help in this instance.

Jul 31, 2009 at 10:23 PM


Where is the LockBehavior property? I could not find it with the "Enterprise Library Configuration" in version 4.1. In fact, I am looking for the solution to unlock the "Rolling Flat File Trace Listener". We have an application which will write the user activities to a log file and the users can view the log from the  same application as well.



Aug 3, 2009 at 8:37 AM
Edited Aug 3, 2009 at 8:38 AM


I tried looking at the source code of the ent lib 3.0, and I also can't find the LockBehavior. What you can do is to modify the the existing behaviour of the rolling flat file to explicitly close the open file after logging to it. What I did is add a single line of code which closes the hold of the trace listener to the file.

This is the method from the RollingFlatFileTraceListener class that I've modified.

public override void TraceData(TraceEventCache eventCache,
                               string source,
                               TraceEventType eventType,
                               int id,
                               object data)

    base.TraceData(eventCache, source, eventType, id, data);

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.