简体   繁体   中英

C# Nlog - How to rotate log files so it creates rotated logs file and archives

I have a trouble with creating a following log rotation settings:

1 - Current log file (service.log)

10 - rotated logs file like service.1.log, service.2.log etc. Log files size are 10MB.

10 - archived logs

When the file reaches 10mb, we discard it with the name.1 when there are 10 such rotated files, the last one is archived and there are also 10 archives.

Example:

  1. I have a log file - service.log. When the file reaches 10mb, we discard it with the 'service.1.log' name and create a new 'service.log' file. Then, when this situation is repeated, we discrad the log file with the 'service.2.log' name.

  2. When there are 10 such rotated files, then the oldest one is archived and so we have the following situation: 1 - service.log (new created file); 9 files - service.2.log, service.3.log, .... service.10.log; 1 archived that was created from the 'service.1.log' file.

  3. We should have also maximun 10 such archives. The oldest archives should be deleted.

Thank you!

Think the closest option is archiveNumbering="Rolling" :

Rolling

<target name="file" xsi:type="File"
    ...
    fileName="service.log"
    archiveFileName="service.{##}.log"
    archiveNumbering="Rolling"
    archiveAboveSize="10000000"
    maxArchiveFiles="10"  />

Example of file names (newest files first):

service.log
service.00.log
service.01.log
service.02.log

See also: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM