30

我们使用 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 中至少有一个#,我宁愿不要。因此,如果您对此有解决方案,我也会感激不尽 =)

4

3 回答 3

53

可能为时已晚,但我相信您需要做的就是使用具有正确日期格式的日期布局渲染器在文件名中包含日期。通过包含日期,您无需指定存档功能。当日期更改时,将自动创建一个新文件。

<?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>
于 2010-06-07T23:56:30.427 回答
8

以防万一有人仍然需要解决方案 - 最近已向 NLog 添加了请求的功能:https ://github.com/NLog/NLog/pull/241 ,但 Nuget 仍然无法使用它

于 2013-09-25T09:19:31.177 回答
-2

也许这就是您需要的,Daily 文件夹,其中包含文件 Log.log

<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
于 2017-10-08T12:08:33.367 回答