NLog File Target

Dec 31, 2012

2 mins read

NLog File Target

輸出到檔案是預設的設定,可以是一個或是多個檔案

參數

File Target可以使用的參數很多,除了特殊需求之外,大部份採用預設值就行了
詳細說明請參考[官網File Target文件](https://github.com/nlog/nlog/wiki/File-target)
以下為常用的參數
  • name - target的名稱
  • layout - 輸出格式,預設為${longdate}|${level:uppercase=true}|${logger}|${message}
  • header - 頭部格式
  • footer - 尾部格式
  • encoding - 檔案編碼,如果有中文字的話,可以指定成utf-8
  • fileName - 檔案名稱,配合Layout Renderers組合檔名

常用範例

非同步,每小時寫檔,UTF8格式

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

&lt;!-- 

See http://nlog-project.org/wiki/Configuration_file for information on customizing logging rules and outputs. –> <targets> <target xsi:type="AsyncWrapper” name="f” queueLimit="5000” overflowAction="Discard”> <target xsi:type="File” fileName="${basedir}/logs/${date:format=yyyy-MM-dd-HH}.log” layout="${longdate} ${uppercase:${level}} ${message}” encoding="utf-8” /> </target> </targets> <rules> <logger name=”*” minlevel="Trace” writeTo="f” /> </rules> </nlog>

CSV格式

<?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">
&lt;!-- 

See http://nlog-project.org/wiki/Configuration_file for information on customizing logging rules and outputs. –> <targets> <target xsi:type="AsyncWrapper” name="f” queueLimit="5000” overflowAction="Discard”> <target xsi:type="File” fileName="${basedir}/logs/${date:format=yyyy-MM-dd-HH}.log” layout="${longdate} ${uppercase:${level}} ${message}” encoding="utf-8” /> </target> </targets> <rules> <logger name=”*” minlevel="Trace” writeTo="f” /> </rules> </nlog>

Sharing is caring!