使用 nlog 在控制台应用程序中滚动文件日志
NLog的文件目标是相当可配置,但它似乎不支持我正在尝试的来实现。
我的控制台应用程序将消息写入日志文件,但我希望它执行两件事:
- 在每次运行时启动一个新的日志文件。因此,每次启动应用程序时,都应创建一个新的日志文件。
- 以前的日志文件不应被覆盖或删除,我也想保留
x
最新运行。
archive* 属性似乎非常接近,但它们是在时间基础上进行滚动的,而不是在执行基础上进行的。
NLog's file target is pretty configurable, but it doesn't seem to support what I am trying to achieve.
My console application writes messages to a log file, but I want it to do two things:
- Start a new log file on each run. So each time the application is started, a new log file should be created.
- Previous log files should not be overwritten or deleted, i want to keep the
x
latest runs as well.
The archive* properties seem to be pretty close, but they do the rolling on a time basis, not an execution basis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个有点简单。将文件名设置为
"log-${cached:${date:format=yyyyMMdd_HH_mm_ss}}.log"
或类似名称。这将为文件设置一个精确到秒的名称,该名称将在每次应用程序执行时缓存。至于你的第二个要求,那就有点困难了。您可以尝试设置
archiveEvery: "None"
和maxArchiveFiles: x
,但我不确定这是否会产生所需的行为,或者根本不会存档任何内容。The first is somewhat easy. Set the filename to
"log-${cached:${date:format=yyyyMMdd_HH_mm_ss}}.log"
or something similar. This will set up the file with a down-to-the-second name, which will be cached on each application execution.As far as your second request, that's a bit more difficult. You might try setting
archiveEvery: "None"
andmaxArchiveFiles: x
, but I'm not sure if that would produce the desired behaviour or just never archive anything at all.