溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

NLog如何配置

發(fā)布時間:2021-08-17 14:18:09 來源:億速云 閱讀:118 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了NLog如何配置,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

NLog是一個基于.NET平臺編寫的類庫,我們可以使用NLog在應(yīng)用程序中添加極為完善的跟蹤調(diào)試代碼。

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

 <targets>
 <target name="asyncFile" xsi:type="AsyncWrapper">
  <target name="log_file" xsi:type="File"
    fileName="${basedir}/Logs/${shortdate}/${shortdate}.txt"
    layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
    archiveFileName="${basedir}/archives/${shortdate}-{#####}.txt"
    archiveAboveSize="102400"
    archiveNumbering="Sequence"
    concurrentWrites="true"
    keepFileOpen="false" />
 </target>
 <target name="console" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
 </targets>

 <rules>
 <logger name="*" minlevel="Error" writeTo="asyncFile" />
 <logger name="*" minlevel="Debug" writeTo="console" />
 </rules>
</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">

 <variable name="logLayout"
   value="Logger:${logger}${newline}Date:${longdate} Level:${uppercase:${level}}${newline}Message:${message} ${newline}${onexception:Exception:${exception:format=toString}${newline}}" />

 <targets>
 <target name="asyncFile" xsi:type="AsyncWrapper">
  <target name="log_file" xsi:type="File"
    fileName="${basedir}/Logs/${shortdate}/${shortdate}.txt"
    layout="${logLayout}"
    archiveFileName="${basedir}/archives/${shortdate}-{#####}.txt"
    archiveAboveSize="102400"
    archiveNumbering="Sequence"
    concurrentWrites="false"
    keepFileOpen="true" 
    encoding="utf-8"
    openFileCacheTimeout="30"/>
 </target>
 </targets>

 <rules>
 <logger name="*" minlevel="Info" writeTo="asyncFile" />
 </rules>
</nlog>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“NLog如何配置”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI