简体   繁体   English

如何设置 NLog 最大文件大小?

[英]How to set NLog max file size?

Is there any option/configuration in NLog to set the max log file size (for example 5MB)? NLog是否有任何选项/配置来设置最大日志文件大小(例如 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?我更喜欢某种内置配置,但如果没有,可以在不破坏日志文件的情况下安全地手动完成吗?

You can set archiveNumbering="DateAndSequence" and archiveAboveSize="5000000"您可以设置archiveNumbering="DateAndSequence"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,请从此处查看此注释

archiveAboveSize - Size in bytes above which log files will be automatically archived. archiveAboveSize - 以字节为单位的大小,超过该大小的日志文件将被自动存档。 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.如果只有一个进程要写入文件,请考虑将 ConcurrentWrites 设置为 false 以获得最佳性能。 Warning: combining this mode with Archive Numbering Date is not supported.警告:不支持将此模式与存档编号日期结合使用。 Archive files are not merged.存档文件不会合并。 DateAndSequence will work DateAndSequence 将起作用

archiveAboveSize should do the trick. archiveAboveSize应该可以解决问题。 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.如果要将 archiveFileName 设置为时间戳,则可以使用${ticks} But I would prefer combining date with sequence number, for better readability.但我更喜欢将日期与序列号结合起来,以获得更好的可读性。

Since version 4.5.7 you can combine archiveAboveSize and archiveNumbering="Date" :从 4.5.7 版开始,您可以组合archiveAboveSizearchiveNumbering="Date"

Date - Date style numbering.日期 - 日期样式编号。 The date is formatted according to the value of archiveDateFormat.日期根据archiveDateFormat 的值进行格式化。

  • Warning: Before NLog ver.警告:在 NLog 版本之前。 4.5.7 then this would not work together with archiveAboveSize. 4.5.7 那么这将无法与 archiveAboveSize 一起使用。 Newer version will correctly merge into the较新的版本将正确合并到
    existing file on archive.存档中的现有文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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