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

Topics: Logging Application Block
Mar 18, 2014 at 7: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.

Thanks!
    [TestMethod]
    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();
        builder.ConfigureLogging()
            .WithOptions
                .DoNotRevertImpersonation()
            .LogToCategoryNamed("Basic")
                .WithOptions
                    .SetAsDefaultCategory()
                .SendTo.RollingFile("Rolling Flat File Trace Listener")
                    .WhenRollFileExists(RollFileExistsBehavior.Increment)
                    .RollEvery(RollInterval.Minute)
                    .RollAfterSize(1000) // 1000 kb
                    .WithTraceOptions(TraceOptions.None)
                    .UseTimeStampPattern("yyyy-MM-dd")
                    .ToFile(logFileFullPath)
                    .FormatWith(new FormatterBuilder()
                                .TextFormatterNamed("Text Formatter")
                                .UsingTemplate(logTemplate))
            ;

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

        EnterpriseLibraryContainer.Current
              = 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);
        content.Should().Contain("START");

        DateTime stopWritingTime = DateTime.Now.AddMinutes(3);
        while (DateTime.Now < stopWritingTime)
        {
            message = DateTime.Now.ToString("G");
            logger.Write(message, category, 1, 0, severity, title);
            Thread.Sleep(900);
        }
    }
Mar 19, 2014 at 6: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
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 19, 2014 at 5: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 20, 2014 at 12:06 AM
Edited Mar 20, 2014 at 12:07 AM
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
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 20, 2014 at 5: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
entlib.support@live.com
Enterprise Library support engineer
Support How-to