log4net使用方法

1.NuGet包管理器中安裝log4netapp

2.接着在項目中添加log4net.config文件,以及手擼或尋找一個LogHelperspa

添加完log4net.config必定要設置屬性,將其始終複製到輸出目錄.net

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="ErrorRollingFileAppender"/>
      <appender-ref ref="WarnRollingFileAppender"/>
      <appender-ref ref="InfoRollingFileAppender"/>
      <appender-ref ref="DebugRollingFileAppender"/>
    </root>

    <!--通常錯誤日誌定義,用於記錄已知需處理的與未捕獲的異常-->
    <!--日誌輸出格式:[時間]:類名 線程號 消息-->
    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR"/>
        <levelMax value="FATAL"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <file value="logs\"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd\\&quot;Error.log&quot;"/>
      <staticLogFileName value="false"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c.%M %t %n%m%n"/>
      </layout>
    </appender>

    <!--警告日誌定義,用於記錄已知不需處理的異常,系統警告信息-->
    <!--日誌輸出格式:[時間]:類名 線程號 消息-->
    <appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="WARN"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <file value="logs\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd\\&quot;Warn.log&quot;"/>
      <staticLogFileName value="false"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %c.%M %t %m%n"/>
      </layout>
    </appender>

    <!--信息日誌定義,用於記錄用戶相關信息-->
    <!--日誌輸出格式:[時間]:消息-->
    <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="INFO"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <file value="logs\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd\\&quot;Info.log&quot;"/>
      <staticLogFileName value="false"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss}] %m%n"/>
      </layout>
    </appender>

    <!--信息日誌定義,用於收集開發調試信息-->
    <!--日誌輸出格式:[時間]:類名 線程號 消息-->
    <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="DEBUG"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <file value="logs\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd\\&quot;Debug.log&quot;"/>
      <staticLogFileName value="false"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss.fff}] %c %t:%m%n"/>
      </layout>
    </appender>

  </log4net>
</configuration>

 

 1     public static class LogHelper  2  {  3         private static readonly log4net.ILog Logerror = log4net.LogManager.GetLogger("DEBUG");  4 
 5         public static void WriteInfoLog(string info)  6  {  7  Logerror.Info(info);  8  }  9 
10         public static void WriteErrorLog(string info) 11  { 12  Logerror.Error(info); 13  } 14 
15         public static void WriteDebugLog(string info) 16  { 17  Logerror.Debug(info); 18  } 19 
20         public static void WriteDebugLog(Exception exception) 21  { 22  Logerror.Debug(exception.Message, exception); 23  } 24     }

 3.最後須要在AssemblyInfo.cs中添加一行代碼線程

 

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

效果以下:3d

1         static void Main(string[] args) 2  { 3             LogHelper.WriteDebugLog("1234444445"); 4         }

 

Demo下載:https://pan.baidu.com/s/1hFXoUPZUWVCwDdjx-bTtHQ調試

相關文章
相關標籤/搜索