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:
Please post full exception details (message, stacktrace, inner exceptions)
BestExLog.txt
Are there any workarounds? yes - write logs to subfolder
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