Rolling Flat File with Fluent API

Topics: Building and extending application blocks, Logging Application Block
Jun 30, 2011 at 2:34 PM
Edited Jun 30, 2011 at 2:46 PM

I am using the Fluent API to handle various configuration options for Logging using EntLib.

I am building up the loggingConfiguration section manually in code. It seems to work great except that the RollingFlatFileTraceListener doesn't actually Roll the file. It will respect the size limit and cap the amount of data it writes to the file appropriately, but it doesn't not actually create a new file and continue the logs.

I've tested it with a sample app and the app.config and it seems to work. So I'm guess that I am missing something although every config option that seems like it needs is there.

Here is the basics of the code (with hard-coded values to show a config that doesn't seem to be working):

//Create the config builder for the Fluent API
var configBuilder = new ConfigurationSourceBuilder();
//Start building the logging config section                 
var logginConfigurationSection = new LoggingSettings("loggingConfiguration"true"General");                 
logginConfigurationSection.RevertImpersonation = false;
var _rollingFileListener = new RollingFlatFileTraceListenerData("Rolling Flat File Trace Listener""C:\\tracelog.log""----------------------""",
                                10, "MM/dd/yyyy"RollFileExistsBehavior.Increment,
                                RollInterval.Day, TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp,
                                "Text Formatter"SourceLevels.All);
                            _rollingFileListener.MaxArchivedFiles = 2;
                            //Add trace listener to current config
                            //Configure the category source section of config for flat file
                            var _rollingFileCategorySource = new TraceSourceData("General"SourceLevels.All);
                            //Must be named exactly the same as the flat file trace listener above.
                            _rollingFileCategorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener"));
                            //Add category source information to current config
	      //Add the loggingConfiguration section to the config.
                configBuilder.AddSection("loggingConfiguration", logginConfigurationSection);
                //Required code to update the EntLib Configuration with settings set above.
                var configSource = new DictionaryConfigurationSource();
                //Set the Enterprise Library Container for the inner workings of EntLib to use when logging
                EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
Any help would be appreciated!
Jul 1, 2011 at 8:10 AM
Edited Jul 1, 2011 at 8:13 AM


Try this code instead:

var myConfig = new ConfigurationSourceBuilder();
                    .SendTo.RollingFile("Rolling Flat File Trace Listener")
                    .WithTraceOptions(TraceOptions.Callstack | TraceOptions.DateTime |
                    TraceOptions.LogicalOperationStack | TraceOptions.ProcessId |
                    TraceOptions.ThreadId | TraceOptions.Timestamp)

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jul 5, 2011 at 11:48 AM

Thank you for the response, unfortunately that isn't working correctly either. It is creating the first file and capping the size but creating any new files after the first one.
I intially thought there may be a permissions issue but that doesn't make sense because the first file is written. It's just subsequent files that are not.

Additionally, I can't use this code long term because I am building up various listeners in code depending on user configuration (setting up a database listener, and either a rolling or non rolling listener)
and if I use this exact code, it will overwrite the other listeners I've set up.
That's why I had this section coded the way I did where I add the section.

There must be something different/missing with the Fluent API and the rolling trace listener that gets set up by default when you are using an app.config?

I appreciate your help.

Jul 5, 2011 at 1:33 PM

A little bit more information.
I realized I was missing the Special Category of Errors & Warnings, so I added that in. I'm seeing errors being logged when it tries to create a new file (see below).

I'm not sure what Ent Lib is doing that is causing the new file name not to be supported. Any ideas?

Exception Information Details:


Exception Type: System.NotSupportedException

Message: The given path's format is not supported.

Data: System.Collections.ListDictionaryInternal

TargetSite: System.String CanonicalizePath(System.String, Boolean)

HelpLink: NULL

Source: mscorlib

StackTrace Information Details:


at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)

at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)

at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)

at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)

at System.IO.File.Move(String sourceFileName, String destFileName)

at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener.StreamWriterRollingHelper.SafeMove(String actualFileName, String archiveFileName, DateTime currentDateTime)

at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener.StreamWriterRollingHelper.PerformRoll(DateTime rollDateTime)

at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener.StreamWriterRollingHelper.RollIfNecessary()

at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)

at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerWrapper.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)

at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter, TraceEventCache traceEventCache)

at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl.ProcessLog(LogEntry log, TraceEventCache traceEventCache);

Category: ;

Priority: -1;

EventId: 6352;

Severity: Error;

Jul 5, 2011 at 2:49 PM

I solved my problem.
The issue is with the string formatter I'm using "MM/dd/yyyy" - you can't have / in file names. I changed them to be hyphens instead and it works properly.