NLog.config 範例

這個 NLog.config 是我自己常用的,貼上來方便自己日後查找剪貼。

詳細的 log 訊息常常幫了我很多忙。如果當初沒有埋一堆除錯訊息到 log,實在很難想像如何研判一些系統上線後的奇怪問題,尤其是客戶的主機遠在另一個不同時區的國家。

底下這個 NLog 組態檔,是我在幾個專案中使用的範本,對我來說已算是挺好用的了。

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

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->

    <target name="file" xsi:type="File"
          layout="${longdate} | ${level:uppercase=true} | ${logger} | ${message} ${onexception:${newline}${exception:format=tostring}}"
          fileName="${basedir}/Logs/logfile.txt"
          archiveFileName="${basedir}/Logs/archives/log.{#}.txt"
          archiveEvery="Day"
          archiveNumbering="Rolling"
          maxArchiveFiles="7"
          concurrentWrites="true"
          keepFileOpen="false"
          encoding="UTF-8" />

    <target name="eventLog" xsi:type="EventLog"
            layout="${longdate} | ${level:uppercase=true} | ${logger} | ${message} ${onexception:${newline}${exception:format=tostring}}"
            source="[[應用程式名稱]]"
            log="Application" />

    <target name="mail" xsi:type="Mail"
            smtpServer="[[SMTP主機名稱]]"
            smtpPort="25"
            smtpAuthentication="None"
            smtpUserName=""
            smtpPassword=""
            enableSsl="false"
            from="[[寄件人]]&lt;[[寄件人信箱]]&gt;"
            to="[[收件人信箱]]"
            html="false"
            encoding="UTF-8"
            addNewLines="true"
            subject="[[應用程式名稱]]:${machinename} 於 ${shortdate} ${time} 產生 ${level} 等級的紀錄"
            header="========================================================================="
            body="時間:${longdate}
            ${newline}Log等級:${level:uppercase=true}
            ${newline}Logger:${logger}
            ${newline}Source:${callsite:className=true}
            ${newline}Exception類別:${exception:format=type}
            ${newline}錯誤訊息:${message}
            ${newline}${onexception:${exception:format=tostring}} ${newline}"
            footer="========================================================================="
            />
  </targets>

  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Debug" writeTo="file" />
    <logger name="*" minlevel="Warn" writeTo="file,eventLog" />
    <logger name="*" minlevel="Error" writeTo="file,eventLog,mail" />
  </rules>
</nlog>

其中以 [[ 和 ]] 包住的文字代表要手動替換的內容。其餘各項設定,這裡就不解釋了(Sorry!),可查看官網的說明

沒有留言:

技術提供:Blogger.
回頂端⬆️