NLog Config

紀錄一下config 用法

File target
layout
高負載 .NET Logging 議題與 NLog 極端效能調校(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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

    <!--[變數] 文字樣板 
2021-03-31 09:56:01.8176 | FATAL | TestWebApi.Controllers.HomeController | 這是Fatal | System.DivideByZeroException: 嘗試以零除。
   於 TestWebApi.Controllers.HomeController.Index() 於 C:\Users\sam.lin\source\repos\TestCode\TestWebApi\Controllers\HomeController.cs: 行 20
  -->
    <variable name="Layout" value="${longdate} | ${level:uppercase=true} | ${logger} | ${message}"/>
    <variable name="LayoutFatal" value="${longdate} | ${level:uppercase=true} | ${logger} | ${message} | ${exception:format=tostring}"/>
    <!--
Machinename:DP-User1               
Basedir:C:\Users\User\source\repos\TestCode\TestWebApi\               
logger:TestWebApi.Controllers.HomeController               
ActivityId:             
Date:2021/03/31 09:23:24.319             
Callsite:TestWebApi.Controllers.HomeController.Index              
Level:Error             
Message:這是Error              
exceptionTostring:System.DivideByZeroException: 嘗試以零除。
   於 TestWebApi.Controllers.HomeController.Index() 於 C:\Users\User\source\repos\TestCode\TestWebApi\Controllers\HomeController.cs: 行 20               
exception:嘗試以零除。               
  -->
    <variable name="CustomLayout" value="Machinename:${machinename}  
            ${newline}Basedir:${basedir}  
            ${newline}logger:${logger}  
            ${newline}ActivityId:${ActivityId}
            ${newline}Date:${Date}
            ${newline}Callsite:${callsite} 
            ${newline}Level:${level}
            ${newline}Message:${message} 
            ${newline}exceptionTostring:${exception:format=tostring}  
            ${newline}exception:${exception}  
            ${newline}-----------------------------------------------------------" />
    <!--[變數] 檔案位置 
  ${basedir} = C:\Users\user\source\repos\TestCode\TestWebApi\
  ${shortdate}.log = 2021-03-31.log
  -->
    <variable name="LogTxtDir" value="${basedir}/Logs/"/>
    <variable name="LogTxtLocation" value="${LogTxtDir}/${shortdate}.log"/>
    <variable name="LogTxtLocationFatal" value="${LogTxtDir}/${shortdate}Error.log"/>

    <!--[設定] 寫入目標
  maxArchiveFiles 應保留的最大存檔文件數。如果maxArchiveFiles小於或等於0,則不會刪除舊文件。整數默認值:0
  archiveNumbering 對文件檔案進行編號的方式
  keepFileOpen 指示是否保持日誌文件打開,而不是在每個日誌記錄事件上打開和關閉它。將此屬性更改為true可以大大提高性能
  但也可以使文件句柄保持鎖定。啟用此選項時,請考慮將openFileCacheTimeout設置為30
  因為它將允許存檔操作並對刪除的日誌文件做出反應
  archiveAboveSize-大小(以字節為單位),如果超過該大小,將自動歸檔日誌文件
  1048576 (1M)
  5242880 (5M)
  archiveFileName-用於存檔的文件名
    -->
    <targets>
        <target name="File" xsi:type="File" fileName="${LogTxtLocation}" layout="${Layout}"
                        encoding="utf-8" maxArchiveFiles="30" archiveNumbering="Sequence"
                        archiveAboveSize="10485760" archiveFileName="${LogTxtDir}/${shortdate}.{##}.log"
              keepFileOpen="true" ConcurrentWrites="false" AutoFlush="false" OpenFileFlushTimeout="1"/>

        <target name="FileFatal" xsi:type="File" fileName="${LogTxtLocationFatal}" layout="${LayoutFatal}"
                    encoding="utf-8" maxArchiveFiles="30" archiveNumbering="Sequence"
                    archiveAboveSize="10485760" archiveFileName="${LogTxtDir}/${shortdate}Error.{##}.log"
            keepFileOpen="true" ConcurrentWrites="false" AutoFlush="false" OpenFileFlushTimeout="1"   />

        <target name="lifetimeConsole"  xsi:type="Console" layout="${level:truncate=4:lowercase=true}: ${logger}[0]${newline}      ${message}${exception:format=tostring}" />

    </targets>

    <!--[設定] 紀錄規則-->
    <rules>
        <logger name="*" levels="Trace,Debug,Info,Warn" writeTo="File,lifetimeConsole" />
        <logger name="*" levels="Error,Fatal" writeTo="FileFatal,lifetimeConsole" />

    </rules>

</nlog>

如果內容有誤請多鞭策謝謝