ロギングが行われた日付でNlogアーカイブをファイルにする方法

StackOverflow https://stackoverflow.com/questions/1635973

  •  06-07-2019
  •  | 
  •  

質問

ログフレームワークとして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に少なくとも1つは必要だと思われますが、そうではありません。あなたがその解決策を得た場合、私は感謝の2倍になるでしょう=)

役に立ちましたか?

解決

誰かがまだ解決策を必要としている場合に備えて-要求された機能が最近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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top