2016-11-09 95 views
2

我正在使用NLog日誌記錄框架。我的應用程序需要保留最近14天的日誌。NLog日常檔案限制大小

我使用的當前NLog.config如下所示。

<targets> 
    <target name="MyFile" 
      xsi:type="File" 
      fileName="C:\Logs\MyApp.log" 
      encoding="utf-8" 
      layout="${date:format=yyyyMMddHHmmss} ${message}" 
      archiveEvery="Day" 
      archiveFileName="C:\Logs\MyApp.{#}.log" 
      archiveNumbering="Date" 
      archiveDateFormat="yyyy-MM-dd" 
      maxArchiveFiles="14" /> 
</targets> 

的問題是,我的應用程序產生大量日誌條目,有時每天的日誌在尺寸上走高於1 GB。如果日日誌的大小限制超過100 MB,是否有可能保留14天結構的每日歸檔並添加另外存檔單日的新子組。

所以最終的日誌輸出會是這個樣子

MyApp.2016-10-01_1 // (100mb limit reached) 
MyApp.2016-10-01_2 
MyApp.2016-10-02 
MyApp.2016-10-03_1 // (100mb limit reached) 
MyApp.2016-10-03_2 // (100mb limit reached) 
MyApp.2016-10-03_3 
... 
MyApp.2016-10-14 

回答

1

是,通過archiveNumbering=DateAndSequencearchiveAboveSize

例如

100MB = 104857600個字節

<targets> 
    <target name="MyFile" 
      xsi:type="File" 
      fileName="C:\Logs\MyApp.log" 
      encoding="utf-8" 
      layout="${date:format=yyyyMMddHHmmss} ${message}" 
      archiveEvery="Day" 
      archiveFileName="C:\Logs\MyApp.{#}.log" 
      archiveNumbering="DateAndSequence" 
      archiveDateFormat="yyyy-MM-dd" 
      archiveAboveSize="104857600" 
      maxArchiveFiles="14" /> 
</targets> 

文件名是:

MyApp.2016-10-01.1 // (100mb limit reached) 
MyApp.2016-10-01.2 
MyApp.2016-10-02.1 
MyApp.2016-10-03.1 // (100mb limit reached) 
MyApp.2016-10-03.2 // (100mb limit reached) 
MyApp.2016-10-03.3 
... 
MyApp.2016-10-14.1