zoukankan      html  css  js  c++  java
  • log4net使用方法

    1.NuGet包管理器中安装log4net

    2.接着在项目中添加log4net.config文件,以及手撸或寻找一个LogHelper

    添加完log4net.config一定要设置属性,将其始终复制到输出目录

    <?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)]

    效果如下:

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

    Demo下载:https://pan.baidu.com/s/1hFXoUPZUWVCwDdjx-bTtHQ

  • 相关阅读:
    CF1312G Autocompletion
    UOJ#11. 【UTR #1】ydc的大树
    CF51F Caterpillar
    CF295D Greg and Caves
    CF288E Polo the Penguin and Lucky Numbers
    CF401D Roman and Numbers
    CF543D Road Improvement
    CF938F Erasing Substrings
    [AGC024E] Sequence Growing Hard
    CF261D Maxim and Increasing Subsequence
  • 原文地址:https://www.cnblogs.com/LikeHeart/p/9353093.html
Copyright © 2011-2022 走看看