2009-10-28 187 views
24

我們使用Nlog作爲我們的日誌框架,並且找不到一種按照自己想要的方式歸檔文件的方法。我想記錄日誌文件名中的日期。
Ex所有從2009-10-01 00:00 -> 2009-10-01:23:59發生的日誌記錄應放置在Log.2009-10-01.log中。但是這一天的所有日誌都應該放在Log.log之內,用於拖尾等。如何讓Nlog將文件與日誌發生日期進行歸檔

我使用的當前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

對不起返回到很老的線程文件夾Log.log ...但你有沒有找到一個解決這個? – 2015-02-28 16:42:37

+0

@AndrewJones沒有。我已經轉向了彈性/ logstash和kibana堆棧。 – 2015-03-01 17:05:06

回答

37

可能來不及幫助你,但我相信你所需要做的就是在文件名中使用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> 
+0

不遲到:)我忘了提及,我仍然希望將活動日誌命名爲Log.log以便進行拖尾等操作。所以這個解決方案是好的,但不完美:) – 2010-06-08 07:56:10

+0

此外,此解決方案禁用自動清除舊歸檔日誌文件的可能性... – Verthosa 2018-01-17 14:04:14

0

也許這是你需要什麼, 日報在它

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