我们使用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中至少有一个#,我宁愿不这样做。所以,如果你有一个解决方案,我也会两倍感恩=)
答案 0 :(得分:46)
可能为时已晚,无法帮助您,但我相信您需要做的就是使用date layout renderer并使用正确的date format在文件名中包含日期。通过包含日期,您无需指定存档功能。当日期更改时,将自动创建一个新文件。
<?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>
答案 1 :(得分:7)
如果有人仍然需要解决方案 - 最近已将请求的功能添加到NLog:https://github.com/NLog/NLog/pull/241,但Nuget仍然无法使用
答案 2 :(得分:0)
也许这就是你需要的, 包含文件Log.log的每日文件夹
<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>