Changing the rolling filename pattern from Date to something else like an integer

Topics: Logging Application Block
Oct 25, 2013 at 8:50 AM
I'm using EntLib 6 and want to make rolling log files like: MyLogFile_1.log, and essentially increasing numbers suffix.

Is this possible? Do I have to write custom code for this?
Oct 25, 2013 at 4:35 PM
I think you can get most of the way to where you want to go with the out of the box implementation.

The approach is to not use a Timestamp Pattern but set the File Exists Behavior to Overwrite and Max Archived Files to 2147483647 (or however many files you want to keep). Now when the file is rolled the base file name of MyLogFile.log will result in an archived file name of MyLogFile.1.log, MyLogFile.2.log, etc. The block inserts the '.' so you can't use an underscore as in your example.

The configuration will look like this:
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="MyLogFile.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"
    rollSizeKB="1" timeStampPattern="" maxArchivedFiles="2147483647" />

You can also use constant expression if you wish. E.g. arc\hive (remembering to escape DateTime format tokens).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 25, 2013 at 4:40 PM
How much code would it be to change this? Can this be done via the public api/hooks? Or do I need to edit RollnigFlatFileTraceListener
Oct 25, 2013 at 5:58 PM
What exact change do you want to make? You will either need to make a small change to the Enterprise Library source code, recompile and use your custom build or create a custom trace listener based on the Enterprise Library source (if you wish -- you could write your own implementation).

You can take a look at the custom Rolling Flat File Trace Listener sample on the Enterprise Library V6 Sample Projects page. All of the infrastructure work (configuration classes, resources etc.) is done for you -- you would just have to insert your custom logic in the appropriate location.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to