File rollover fails sometimes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

File rollover fails sometimes

JohnT-3
My app logs to a file which should roll over on the first event after midnight:

  <nlog>
    <targets>
        <target name="file" type="File"
            layout="${longdate} ${level}: ${message}"
            fileName="${basedir}/log/CManager.log"
            ArchiveEvery="Day"
            ArchiveFileName="${basedir}/log/CManager_{#}.log"
            ArchiveNumbering="Rolling"
            MaxArchiveFiles="5"
            keepFileOpen="True"
            encoding="iso-8859-2" />
        <target name="console" type="ColoredConsole"
            layout="${message}" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
        <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
  </nlog>

The application runs on a system with rather slow disk i/o. If an event is logged within a second or so before midnight, NLog will sometimes continue appending to the same file instead of rolling over (sample file available, but too big to upload).

I believe it's because a file write operation initiated before midnight does not compete until afterwards, so the lastWriteTime obtained from GetFileInfo() (called from ShouldAutoArchive()) reflects the new day.

I don't feel comfortable enough with the code to offer a fix (sorry), although a strategy might be to save the time when the last write was initiated, rather than reading it back from the file system.