I'm using NLog logging framework. My application needs to keep archive of last 14 days of logs.
The current NLog.config that I use looks like this.
<targets>
<target name="MyFile"
xsi:type="File"
fileName="C:\Logs\MyApp.log"
encoding="utf-8"
layout="${date:format=yyyyMMddHHmmss} ${message}"
archiveEvery="Day"
archiveFileName="C:\Logs\MyApp.{#}.log"
archiveNumbering="Date"
archiveDateFormat="yyyy-MM-dd"
maxArchiveFiles="14" />
</targets>
The problem is that my application generates lots of log entries and sometimes daily log can go higher than 1 gb in size. Is it possible to keep this daily archive of 14 days structure and add new subgroup that additionally archives single day if the size limit of day log exceeds 100 mb.
So final log output would look something like this
MyApp.2016-10-01_1 // (100mb limit reached)
MyApp.2016-10-01_2
MyApp.2016-10-02
MyApp.2016-10-03_1 // (100mb limit reached)
MyApp.2016-10-03_2 // (100mb limit reached)
MyApp.2016-10-03_3
...
MyApp.2016-10-14
So the NLog archiving feature does not work the way you want it to. It just places the last file of the month to the archive folder not all. This way you will get one file per day and the file will be transferred to the archive folder each day. The archive will keep the file for one year.
${basedir} — Directory where the application runs, aka. AppDomain.BaseDirectory.
Yes, by using archiveNumbering=DateAndSequence
and archiveAboveSize
.
e.g.
100MB = 104857600 bytes
<targets>
<target name="MyFile"
xsi:type="File"
fileName="C:\Logs\MyApp.log"
encoding="utf-8"
layout="${date:format=yyyyMMddHHmmss} ${message}"
archiveEvery="Day"
archiveFileName="C:\Logs\MyApp.{#}.log"
archiveNumbering="DateAndSequence"
archiveDateFormat="yyyy-MM-dd"
archiveAboveSize="104857600"
maxArchiveFiles="14" />
</targets>
file names will be:
MyApp.2016-10-01.1 // (100mb limit reached)
MyApp.2016-10-01.2
MyApp.2016-10-02.1
MyApp.2016-10-03.1 // (100mb limit reached)
MyApp.2016-10-03.2 // (100mb limit reached)
MyApp.2016-10-03.3
...
MyApp.2016-10-14.1
So you have three problems to fix -
You can try this. If you include the ${shortdate} in your file name the file name will be MyApp_yyyyMMdd.log which in itself rolls over every day.
Now to archive on size limitation use the archiveNumbering="Sequence" and archiveAboveSize="your_size_limit_here".
NLog 4.7 introduces a new property 'maxArchiveDays' set this to your value 14.
With all this, the target should look something like,
<target xsi:type="File" name="MyFile"
fileName="C:\Logs\MyApp_${shortdate}.log"
layout="<layout_here>"
....
archiveFileName="C:\Logs\MyApp_${shortdate}_{#}.log"
archiveNumbering="Sequence"
archiveAboveSize="<your_size_limit_here>"
maxArchiveDays="14"/>
Reference: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples#archive-numbering-examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With