c#【Nlog 簡單可用 適合小白】Net項目中NLog的配置和使用實例詳解



前2個安裝下
打開   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">
  <!--項目日志保存文件路徑說明fileName="${basedir}/保存目錄,以年月日的格式創建/${shortdate}/${記錄器名稱}-${單級記錄}-${shortdate}.txt"-->
  <target name="log_file" xsi:type="File"
    fileName="${basedir}/ProjectLogs/${shortdate}/${logger}-${level}-${shortdate}.txt"
    layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
    archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
    archiveAboveSize="102400"
    archiveNumbering="Sequence"
    concurrentWrites="true"
    keepFileOpen="false" />
 </target>
 <!--使用可自定義的着色將日志消息寫入控制台-->
 <target name="colorConsole" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
 </targets>
 
 <!--規則配置,final - 最終規則匹配后不處理任何規則-->
 <rules>
 <logger name="Microsoft.*" minlevel="Info" writeTo="" final="true" />
 <logger name="*" minlevel="Info" writeTo="asyncFile" />
 <logger name="*" minlevel="Warn" writeTo="colorConsole" />
 </rules>
</nlog>
使用:
        public static  Logger _log = LogManager.GetCurrentClassLogger();

            _log.Info("1111");
            _log.Error("2222");
保存位置
在Debug目錄下 ProjectLogs 目錄內
如果沒有。。。。那檢查人品吧   我這踩了好多坑總結出來最簡單的用法了!

參考:https://www.jb51.net/article/165338.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM