15

Is there any option/configuration in NLog to set the max log file size (for example 5MB)?

What I need is, that when the log file exceeds the max size (which I define), It will backup the old one (with a time stamp as file name), and start writing to a new one.

How can this be done? I would prefer some kind of build-in configuration, but if there is none can this be done safely manually without corrupting the log file?

zig
  • 4,524
  • 1
  • 24
  • 68
  • Have you tried this? https://github.com/nlog/NLog/wiki/File-target#archive-old-log-files – Oystein May 24 '18 at 05:54
  • Possible duplicate of [Can I configure NLog to prune logs after they reach a certain limit?](https://stackoverflow.com/questions/19259993/can-i-configure-nlog-to-prune-logs-after-they-reach-a-certain-limit) – kara May 24 '18 at 05:55

3 Answers3

15

You can set archiveNumbering="DateAndSequence" and archiveAboveSize="5000000"

<targets>
  <target xsi:type="File"
          archiveNumbering="DateAndSequence"
          archiveAboveSize="5000000"
          // other config
</targets>

See this note from here if you are using archiveAboveSize

archiveAboveSize - Size in bytes above which log files will be automatically archived. Long Caution: Enabling this option can considerably slow down your file logging in multi-process scenarios. If only one process is going to be writing to the file, consider setting ConcurrentWrites to false for maximum performance. Warning: combining this mode with Archive Numbering Date is not supported. Archive files are not merged. DateAndSequence will work

Julian
  • 33,915
  • 22
  • 119
  • 174
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
2

archiveAboveSize should do the trick. It sets the size (bytes) that will be used as a condition for archiving...
If you want to set archiveFileName to timestamp, ${ticks} can be used. But I would prefer combining date with sequence number, for better readability.

Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18
0

Since version 4.5.7 you can combine archiveAboveSize and archiveNumbering="Date":

Date - Date style numbering. The date is formatted according to the value of archiveDateFormat.

  • Warning: Before NLog ver. 4.5.7 then this would not work together with archiveAboveSize. Newer version will correctly merge into the
    existing file on archive.

https://github.com/nlog/NLog/wiki/File-target#size-based-file-archival

  • Welcome to SO. How is your answer different to the already given and accepted answer? if you think the accepted answer is good, once you have sufficient reputation, you can please vote it up – Guy Levy Feb 11 '21 at 09:14