C# 如何使Nlog存档文件包含日志记录发生的日期

C# 如何使Nlog存档文件包含日志记录发生的日期,c#,nlog,C#,Nlog,我们使用Nlog作为日志框架,我找不到一种方法来按我想要的方式归档文件。我想在日志文件名中记录日志的日期。 例如,从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作为日志框架,我找不到一种方法来按我想要的方式归档文件。我想在日志文件名中记录日志的日期。
例如,从
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中至少有一个文件名,我不希望这样。因此,如果您也能找到解决方案,我将加倍感谢=)

可能太晚了,无法帮助您,但我相信您所需要做的就是在文件名中使用正确的日期。通过包含日期,您无需指定存档功能。当日期更改时,将自动创建一个新文件

<?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>


以防万一,如果有人仍然需要解决方案--请求的功能最近已添加到NLog:,但Nuget仍然无法提供它

也许这就是您需要的, 包含文件Log.Log的每日文件夹

<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>


不要迟到:)我忘了提到我仍然希望活动日志名为log.log以便跟踪等等。因此,这个解决方案很好,但并不完美:)此外,这个解决方案禁用了自动擦除旧存档日志文件的可能性…日期布局渲染器链接已断开,您可以修复它吗?很抱歉,返回到一个非常旧的线程…但您是否找到了解决方案?@AndrewJones Nope。我已经转到elastic/logstash和kibana堆栈。这个配置每天都会出现吗?@Mike Cole Nothing