RollingFlatFileTraceListener doesnt rollover log more than twice when filename has []

Topics: Logging Application Block
Dec 12, 2011 at 9:53 PM

I have the following in app.config

<loggingConfiguration defaultCategory="" logWarningsWhenNoCategoriesMatch="false" name="Logging Application Block" tracingEnabled="True">     
<listeners>       
<add fileName="c:\Application\Log\Server[100].log" formatter="Text Formatter" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="2048" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener" />     
</listeners>
...
</loggingConfiguration>
The log files created are Server[100].log and Server[100].2011-12-12.1.log, but the next rollover doesnt create Server[100].2011-12-12.2.log as expected. 
using Enterprise Library 4.1, I didnot check if 5.0 would fix this. I found that filename without square brackets fixes the problem, i.e. fileName="c:\Application\Log\Server100.log"
Dec 13, 2011 at 1:53 AM
Edited Dec 13, 2011 at 3:42 AM

It looks like you've hit a bug.  

The issue is that in order to calculate the sequence number to append to the file name the code uses a regular expression:

Regex regex = new Regex(string.Format(@"{0}\.(?<sequence>\d+){1}", fileName, extension));

Your fileName happens to contain left square bracket ([) which has a special meaning for a regular expression.  Since the fileName is not escaped
the brackets are treated as part of the regular expression and cause the match to fail and results in the same sequence number being returned for
every file.  This causes the roll file to be overwritten with every roll.  You can cause the issue to occur with a variety of regex characters (e.g. fileName="Server^100.log"). 

The code fix is to escape the fileName and the extension in the regular expression:

Regex regex = new Regex(string.Format(@"{0}\.(?<sequence>\d+){1}", Regex.Escape(fileName), Regex.Escape(extension)));                

Unfortunately, moving to Enterprise Library 5 doesn't help since this code is the same in both versions.

I can't think of a good workaround short of not using the following regex special characters in your file names: +,{, [, (,), ^, $ or creating your own RollingFlatFileTraceListener
or modifying the Enterprise Library source and rebuilding. Other regex special characters would be affected as well but they are actually illegal as a file name (e.g. *).

 

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Dec 13, 2011 at 2:06 AM

I have added this to the issue tracker: http://entlib.codeplex.com/workitem/31932

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com