How to set NLog max file size?

狂风中的少年 提交于 2020-08-24 06:43:06

问题


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?


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/50501773/how-to-set-nlog-max-file-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!