Yet another - rolling file logging not rolling (using Fluent API)

Topics: Logging Application Block
Mar 18, 2014 at 6:29 PM
I am using the Fluent API to configure my rolling flat file listener. It writes to the file no problem, but I simply cannot get it to roll! Can anyone see what the problem is? Is it a bug with EntLib?

Additional notes - using EntLib5, running on Windows Server 2012, on VS2012.

    public void Should_have_log_files_roll_using_fluent_api()

        // specify the target log file name and path
        string rootDir = ConfigurationManager.AppSettings.Get("LogFilePath");
        string logTemplate = ConfigurationManager.AppSettings.Get("LogTemplate"); // e.g. <add key="LogTemplate" value="{timestamp(local:MM/dd/yyyy HH:mm:ss.fffffff)} {severity} | {title} - {message}"/>
        string logFileFullPath = Path.Combine(rootDir, "sample_rolling_file.log");

        // configure logging using Fluent API
        var builder = new ConfigurationSourceBuilder();
                .SendTo.RollingFile("Rolling Flat File Trace Listener")
                    .RollAfterSize(1000) // 1000 kb
                    .FormatWith(new FormatterBuilder()
                                .TextFormatterNamed("Text Formatter")

        // override the default .config file configuration to use above configuration
        var configSource = new DictionaryConfigurationSource();

              = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

        // create a new instance of the logger 
        LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
        // LogWriterFactory logWriterFactory = new LogWriterFactory(configSource);
        // _logWriter = logWriterFactory.Create();

        //--- TEST WRITING

        string category = "Basic";
        TraceEventType severity = TraceEventType.Information;
        string message = "START";
        string title = "Unit Test";

        logger.Write(message, category, 1, 0, severity, title);
        string content = File.ReadAllText(logFileFullPath);

        DateTime stopWritingTime = DateTime.Now.AddMinutes(3);
        while (DateTime.Now < stopWritingTime)
            message = DateTime.Now.ToString("G");
            logger.Write(message, category, 1, 0, severity, title);
Mar 19, 2014 at 5:48 AM
The posted code is running fine for me. What if you change the rootDir to a directory that doesn't exist (and let the block create the folder). With short refresh intervals file information can be cached which causes incorrect behavior of the RollingFlatFileTraceListener.

Randy Levy
Enterprise Library support engineer
Support How-to
Mar 19, 2014 at 4:57 PM
Thank you for trying out the code Randy.

It bother's me greatly that I can't get this to work on my machine, but yet you indicate the code is fine. Even when I change it to a new non-existent directory, I cannot get the file to roll... it always writes to the exact same file. I'm really at a loss and not sure what I should be doing / testing next.

With regards to short intervals, what I had originally done in my tests was to change my server's clock, hoping that would trigger a new file, and that didn't happen either.

Please, if anyone has suggestions on what to look at next, it would be greatly appreciated. Thank you.
Mar 19, 2014 at 11:06 PM
Edited Mar 19, 2014 at 11:07 PM
I would recommend creating a sample project that reproduces the behavior -- it might help shed some light on the issue. Assuming the code is OK then the issue might be environmental (e.g. permissions)

Randy Levy
Enterprise Library support engineer
Support How-to
Mar 20, 2014 at 4:35 AM
Could you be getting a System.IO.IOException on File.ReadAllText(logFileFullPath); (due to the file being locked) which causes the rolling portion of the test to not execute?

Randy Levy
Enterprise Library support engineer
Support How-to