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

How to set NLog max file size?

NLog中是否有option/configuration设置最大日志文件大小(例如5MB)?

我需要的是,当日志文件超过最大大小(我定义的)时,它将备份旧的(以时间戳作为文件名),并开始写入新的。

如何做到这一点? 我更喜欢某种内置配置,但是如果有 none 可以在不损坏日志文件的情况下安全地手动完成吗?

archiveAboveSize 应该可以解决问题。它设置将用作存档条件的大小(字节)...
如果要将archiveFileName设置为时间戳,可以使用${ticks}。但我更喜欢将日期与序列号结合起来,以获得更好的可读性。

您可以设置archiveNumbering="DateAndSequence"archiveAboveSize="5000000"

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

如果您正在使用 archiveAboveSize,请参阅 here 中的这条注释

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

从版本 4.5.7 开始,您可以组合使用 archiveAboveSizearchiveNumbering="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