在我的一些程序中,我按照预期进行了 30 天的轮换,其中包含几个月的多个条目,但只有 30 个日志文件。在其他程序中,我只有一天的日志文件,但总共仍然只有 30 个日志文件。我想要的只是过去 30 天的日志文件条目以及 30 个日志文件。我想我不知道我错过了什么。
昨天,当程序启动备份时,我的一个日志文件被覆盖,因此我丢失了可以告诉我发生了什么情况的数据。那么我的第二个问题是,归档是否只是删除不符合模式的文件,还是实际上获取日志文件并将它们放在某个地方?归档到底是什么?这是我的 nlog.config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<variable name="LogDir" value="${specialfolder:folder=MyDocuments}/MyApp/Log"/>
<variable name="LogDay" value="${date:format=dd}"/>
<targets async="true">
<!-- add your targets here -->
<target name="LogTarget1"
xsi:type="File"
fileName="${LogDay}.log"
encoding="utf-8"
maxArchiveFiles="30"
archiveNumbering="Sequence"
archiveAboveSize="52428800"
archiveFileName="${LogDay}.{#######}.log"
layout="${date:format=MM/dd/yyyy HH\:mm\:ss}|${level:uppercase=true}|${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" writeTo="LogTarget1" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
这是 NLog 的一个缺点,没有得到很好的记录。
当应删除文件(例如最大存档文件)时,日志文件和存档文件不能位于同一文件夹中。
因此,此配置的一个修复方法是将 archifeFilePath 更改为
archiveFileName="archive/${LogDay}.{#######}.log"
Run Code Online (Sandbox Code Playgroud)
另请参阅GitHub 上的此问题