Rolling Flat File listener issue

Topics: Logging Application Block
Jan 31, 2012 at 1:59 PM

I am configuring a Rolling Flat File trace listener with Fluent API.  I pass in the file name and it creates it.  But it only will log one entry in that file and it will also create 2 other log files.. these 2 other files name's have the file name passed in plus a Guid value.  Is this normal of the EntLib 5.0 LAB?

Here is my configuration 

var logFileListener = new RollingFlatFileTraceListenerData
	("Rolling Flat File Trace Listener", _logFileName,
	 "------------------------------","", 1024,"MM-dd-yyyy"
	RollFileExistsBehavior.Increment, RollInterval.Day,
	TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp,                                                                         
	"Text Formatter");
logFileListener.MaxArchivedFiles = 2;            
var logFileCatergorySource = new TraceSourceData("LogFile", _sourceFilter);            
logFileCatergorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener"));

Feb 1, 2012 at 12:03 AM

The posted code does not look like it's using the fluent interface.

Based on the source code above it looks like you want to configure the trace listener like this:

            var builder = new ConfigurationSourceBuilder();

                     .SendTo.RollingFile("Rolling Log File")
                       .FormatWith(new FormatterBuilder()
                         .TextFormatterNamed("Text Formatter")
                           .UsingTemplate("Message: {message} Timestamp: {timestamp}...{newline})}"))
                         .WithTraceOptions(TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp)

Randy Levy
Enterprise Library support engineer 

Feb 1, 2012 at 12:26 PM


I guess I am not using Fluent then but this doesn't answer my question. All that is, is the code I gave presented using the Fluent API.

 Why does my code create multiple log files? It is creating a file with the name I pass to it and it also creates 2-3 files where the names are Guid + filename ( ex.  file name I pass is 'applog.log' and it creates an 'applog.log' AND '1d35983d-a8a9-491a-820e-2007976bc2f8applog.log')



Feb 1, 2012 at 1:33 PM

The reason why you get multiple files is that you are using multiple trace listeners that all point to the destination same file.  Each trace listener locks (or will attempt to lock) the file exclusively.  If it fails to obtain the lock it will create a new file prepended with a GUID.  So you shouldn't create more than one trace listener that points to the same file.

The "Data" classes (e.g. RollingFlatFileTraceListenerData) are typically used for reading the configuration information by Enterprise Library.  They can be used to configure the block but since version 5 the preferred method for runtime configuration is to use the fluent interface.


Randy Levy
Enterprise Library support engineer

Feb 1, 2012 at 1:47 PM

Multiple Trace Listeners? umm where am I setting up multiple trace listeners that are accessing that file?  I read in another forum "The RollingFlatFileListener is based on the the .NET Framework's TextWriterTraceListener, which defines this prefixing behavior: 'If an attempt is made to write to a file that is in use or unavailable, the file name is automatically prefixed by a GUID.'"

Since Fluent is the preferred method (and a method I would like to use) how can I dynamically configure logging if I have to do conditional statements of certain factors? IE I want to check if the end user is providing a log file name, if they don't then it shouldn't configure logging to a file and will default to logging to the EventLog.

When I originally tried something like this it wouldn't work

Feb 3, 2012 at 2:05 AM

Multiple trace listeners locking is almost always the cause of that issue but as you say it could be other issues.

It's hard to know exactly what is going on based on the code snippet.  I would guess that you call a method the same file name is passed in each time so a new trace listener is created each time pointing to the same time which causes locking issues.  It would be easier with a full example that recreated the issue.

Is your configuration a one time setup done at application initialization or during the first call or on the fly?   

Randy Levy
Enterprise Library support engineer 

Feb 3, 2012 at 12:10 PM

Now I understand what you meant by multiple trace listeners.  What I was doing is, I was creating a current instance of LogWriter (as seen below)


and that was happening when each time I switched between pages in my application, so there were multiple instances being started.  This has been resolved and is working good now.  Thank you

Aug 21, 2012 at 9:02 PM

HI, do we have a fix for the multiple log files being created. IE GUID+"mylogfilename.log".  Pls let me know if there is a fix for this issue. I want only one log file to be generated.

Aug 22, 2012 at 1:32 AM

There is specific work item scenario which was resolved.  However anytime there is file locking the System.Diagnostics classes will prepend the filename with a GUID so that behavior still exists.  

So the question is what is the exact scenario where you are seeing the the GUID issue.  I would recommend starting a new thread with the pertinent information to recreate the issue and we can sort out what is going on.


Randy Levy
Enterprise Library support engineer