Nlog: FileTarget deleting incorrect files (including exe.config) when rolling file over

Created on 1 Mar 2018  路  5Comments  路  Source: NLog/NLog

Bug
NLog version: (e.g. 4.4.12)

Platform: .Net 4.5

Current NLog config (xml or C#, if relevant)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile=".\BestExLog.txt">
    <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
    <variable name="myvar" value="myvalue" />
    <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
    <targets>
      <target xsi:type="Console" name="Console" />
      <target xsi:type="File" name="File" fileName=".\BextExLogFile.log" keepFileOpen="false" archiveEvery="Month" maxArchiveFiles="12" archiveNumbering="Sequence" archiveFileName=".\BextExLogFile_${shortdate}.log" layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}">
      </target>
      <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->
      <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
    </targets>
    <rules>
      <!-- add your logging rules here -->
      <logger name="*" minlevel="Trace" writeTo="File" />
      <logger name="*" minlevel="Error" writeTo="Console" />
      <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
    </rules>
  </nlog>

In case of a BUG:

  • What is the current result?
    When a file rolls over - in this case 1 March as archiveEvery="Month" - nlog tries to delete files that are not logs. This includes the application exe, which thankfully fails because it is in use obviously, but it does successfully delete the exe.config, which causes the app to fail catastrophically
  • What is the expected result?
    Only the log files older than the most recent 12 should be deleted
  • Did you checked the Internal log?
    Yes
  • Please post full exception details (message, stacktrace, inner exceptions)
    BestExLog.txt

  • Are there any workarounds? yes - write logs to subfolder

  • Is there a version in which it did work? No
  • Can you help us by writing an unit test? Perhaps!
file-target question

All 5 comments

This is not supported (Should never use date-renderer in archiveFileName):

archiveFileName=".\BextExLogFile_${shortdate}.log"

You should always specify {#} when using any of the custom archiveNumbering-methods. I recommend like this:

archiveFileName=".\BextExLogFile_{#}.log"
archiveNumbering=DateAndSequence 

And maybe configure archiveDateFormat if you don't like the default.

Thanks for the very quick response! This has been a very frustrating issue to nail down, I think it would save others a lot of grief if it was fixed - maybe just throw an exception if this option is used?

You are right, when the archiveFileName is configured wrong, then it should just stop itself from doing havoc.

You have a friend that also experienced the pain, when having configured archiveFileName wrong here: #2606

@nonpoliticaltag NLog 4.5 already fixes this by defaulting to a much more gentle archive-logic, which doesn't try to delete all files in the archive folder.

Will add some logging when having specified archiveNumbering <> Sequence, and having forgotten to include {#}

Btw. duplicate of #1670 and resolved by #1993

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geedsen picture geedsen  路  3Comments

MaximRouiller picture MaximRouiller  路  3Comments

Rapiiidooo picture Rapiiidooo  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments

Jerefeny picture Jerefeny  路  3Comments