欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#快速配置NLog日志的教程詳解

 更新時間:2024年02月20日 10:43:10   作者:搬磚的詩人Z  
這篇文章主要為大家詳細介紹了C#快速配置NLog日志的教程相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下

首先我們需要在Nuget中安裝Nlog和Nlog-Schema。

添加配置文件: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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off"
      internalLogFile="d:\nlog\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <!--<variable name="myvar" value="myvalue"/>-->

  <variable name="logDir" value="${basedir}/nlog"/>
  <variable name="logFileName" value="${date:format=yyyyMMdd}.txt"/>
  <variable name="logArchiveFileName" value="${date:format=yyyyMMdd}_{#}.txt"/>
  <variable name="logLayout" value="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} [${level}] ${message}"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->

  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->

    <target xsi:type="File" name="info"
            layout="${logLayout}"
            fileName="${logDir}/info/${logFileName}"
            archiveFileName="${logDir}/info/${logArchiveFileName}"
            archiveAboveSize="10485760"
            archiveNumbering="Sequence"
            maxArchiveFiles="100"
            concurrentWrites="true"
            keepFileOpen="true"
            openFileCacheTimeout="30"
            encoding="UTF-8" />

    <target xsi:type="File" name="debug"
            layout="${logLayout}"
            fileName="${logDir}/debug/${logFileName}"
            archiveFileName="${logDir}/debug/${logArchiveFileName}"
            archiveAboveSize="10485760"
            archiveNumbering="Sequence"
            maxArchiveFiles="100"
            concurrentWrites="true"
            keepFileOpen="true"
            openFileCacheTimeout="30"
            encoding="UTF-8" />

    <target xsi:type="File" name="error"
            layout="${logLayout}"
            fileName="${logDir}/error/${logFileName}"
            archiveFileName="${logDir}/error/${logArchiveFileName}"
            archiveAboveSize="10485760"
            archiveNumbering="Sequence"
            maxArchiveFiles="100"
            concurrentWrites="true"
            keepFileOpen="true"
            openFileCacheTimeout="30"
            encoding="UTF-8" />

    <target xsi:type="File" name="warn"
        layout="${logLayout}"
        fileName="${logDir}/warn/${logFileName}"
        archiveFileName="${logDir}/warn/${logArchiveFileName}"
        archiveAboveSize="10485760"
        archiveNumbering="Sequence"
        maxArchiveFiles="100"
        concurrentWrites="true"
        keepFileOpen="true"
        openFileCacheTimeout="30"
        encoding="UTF-8" />

  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->

    <logger name="*" minlevel="Info" maxlevel="Info" writeTo="info" />

    <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debug" />

    <logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" />

    <logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="warn" />

  </rules>
</nlog>

定義了5種log類型Debug,Info,Debug,Error,Warn,詳細配置可以自己定義。

定義一個常用的log類

  public class NLogHelper
  {
      private static Logger _log = NLog.LogManager.GetCurrentClassLogger();

      /// <summary>
      /// Debug日志
      /// </summary>
      /// <param name="log"></param>
      public static void Debug(string log)
      {
          _log.Debug(log);
      }

      /// <summary>
      /// Error日志
      /// </summary>
      /// <param name="log"></param>
      public static void Error(string log)
      {
          _log.Error(log);
      }

      /// <summary>
      /// Warn日志
      /// </summary>
      /// <param name="log"></param>
      public static void Warn(string log)
      {
          _log.Warn(log);
      }

      /// <summary>
      /// Info日志
      /// </summary>
      /// <param name="log"></param>
      public static void Info(string log)
      {
          _log.Info(log);
      }

      /// <summary>
      /// 詳細異常日志
      /// </summary>
      /// <param name="ex"></param>
      public static void Exception_Error(Exception ex)
      {
          try
          {
              if (ex != null)
              {
                  StringBuilder strBuilder = new StringBuilder();
                  strBuilder.Append("【異常日志消息】");
                  strBuilder.AppendLine(ex.Message);
                  strBuilder.Append("【異常日志Trace】");
                  strBuilder.AppendLine(ex.StackTrace);
                  strBuilder.Append("【異常日志全部】");
                  strBuilder.Append(ex.ToString());
                  _log.Error(strBuilder.ToString());
              }
          }
          catch (Exception)
          {
          }
      }
  }

調用代碼

   NLogHelper.Info($"系統正在運行中...");

到此這篇關于C#快速配置NLog日志的教程詳解的文章就介紹到這了,更多相關C#配置NLog日志內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#使用selenium實現爬蟲

    C#使用selenium實現爬蟲

    這篇文章介紹了C#使用selenium實現爬蟲的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#不登錄電腦啟動程序

    C#不登錄電腦啟動程序

    本文主要介紹了創(chuàng)建系統服務;開啟服務,啟動程序。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • C# pictureBox用法案例詳解

    C# pictureBox用法案例詳解

    這篇文章主要介紹了C# pictureBox用法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • C# IEnumerator枚舉器的具體使用

    C# IEnumerator枚舉器的具體使用

    本文主要介紹了C# IEnumerator枚舉器的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • C#實現簡易猜數字游戲

    C#實現簡易猜數字游戲

    這篇文章主要為大家詳細介紹了C#實現簡易猜數字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • C#自定義事件監(jiān)聽實現方法

    C#自定義事件監(jiān)聽實現方法

    這篇文章主要介紹了C#自定義事件監(jiān)聽實現方法,涉及C#事件監(jiān)聽的實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • c#之圓形無標題欄橢圓窗體的實現詳解

    c#之圓形無標題欄橢圓窗體的實現詳解

    本篇文章是對c#中圓形無標題欄橢圓窗體的實現方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • C#中TransactionScope的使用小結

    C#中TransactionScope的使用小結

    本篇文章主要是對C#中TransactionScope的使用方法進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • c# 遍歷 Dictionary的四種方式

    c# 遍歷 Dictionary的四種方式

    這篇文章主要介紹了c# 遍歷 Dictionary的四種方式,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-09-09
  • 3種C# 加載Word的方法

    3種C# 加載Word的方法

    本次經驗內容分享通過C#程序來加載Word文檔的3種不同方法。分別是:加載本地Word文檔、以只讀模式加載Word文檔、從流加載Word 想具體了解的小伙伴請參考下文
    2021-09-09

最新評論