John Titor
John Titor

Reputation: 391

NLog limit size of daily archive

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

Upvotes: 15

Views: 17575

Answers (2)

Priyanka
Priyanka

Reputation: 41

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

Upvotes: 4

Julian
Julian

Reputation: 36700

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

Upvotes: 24

Related Questions