Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog limit size of daily archive

Tags:

.net

logging

nlog

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
like image 770
John Titor Avatar asked Nov 09 '16 16:11

John Titor


People also ask

How does NLog archive work?

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.

What is Basedir in NLog?

${basedir} — Directory where the application runs, aka. AppDomain.BaseDirectory.


2 Answers

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
like image 68
Julian Avatar answered Sep 28 '22 04:09

Julian


So you have three problems to fix -

  1. Create a new .log file every day.
  2. Add a size limitation per file.
  3. Retain max of 14 days worth of logs.

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

like image 41
Priyanka Avatar answered Sep 28 '22 06:09

Priyanka