题
我们使用Nlog作为我们的日志框架,我找不到按照我想要的方式存档文件的方法。我想知道日志记录文件名中的日志记录日期。
Ex从 2009-10-01 00:00发生的所有日志记录 - > 2009-10-01:23:59
应放在 Log.2009-10-01.log
中。但是今天的所有日志都应该放在 Log.log
中进行拖尾等等。
我使用的当前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" >
<extensions>
<add assembly="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/Log.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
archiveEvery="Day"
archiveFileName="${basedir}/Logs/Log${shortdate}-{#}.log"
archiveNumbering="Sequence"
maxArchiveFiles="99999"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
但是,这会将日志文件中的日期设置为创建新日志文件的日期。当您想稍后阅读日志时,这会导致挫败感。
似乎我必须在archiveFileName中至少有一个#,我宁愿不这样做。所以,如果你有一个解决方案,我也会两倍感恩=)
解决方案
如果有人仍然需要解决方案 - 最近已向NLog添加了请求的功能: https://github.com/NLog/NLog/pull/241 ,但Nuget仍然无法使用
其他提示
可能为时已晚,无法帮助您,但我相信您需要做的就是使用日期布局渲染器,使用正确的日期格式 。通过包含日期,您无需指定存档功能。当日期更改时,将自动创建一个新文件。
<?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" >
<extensions>
<add assembly="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
也许这就是你需要的, 包含文件Log.log的每日文件夹
<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
不隶属于 StackOverflow