Log4Net日志组件

1.Nuget下载Log4Net包

2.设置应用程序配置文件App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   
  <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="InfoRollingFileAppender" />
      <appender-ref ref="WarnRollingFileAppender" />
      <appender-ref ref="ErrorRollingFileAppender" />
    </root>
    <!--log-->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="D:\Log\"/>
      <param name="AppendToFile" value="true"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="yyyyMMdd/'Log_'yyyyMMdd&quot;.log&quot;"/>
      <param name="RollingStyle" value="Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
      </layout>
    </appender>
    <!--Info-->
    <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="D:\Log\" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMMdd/'LogInfo_'yyyyMMdd&quot;.log&quot;" />
      <param name="StaticLogFileName" value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] 【%-5p】 %c [%x]:[line%line]- %m%n"/>
       <!--conversionPattern value="%date;%file;行号:%line;线程ID:[%thread];%message %newline" /> -->
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      </filter>
    </appender>
    <!--WARN-->
    <appender name="WarnRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="D:\Log\" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMMdd/'LogWarn_'yyyyMMdd&quot;.log&quot;" />
      <param name="StaticLogFileName" value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x]  - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="WARN" />
        <param name="LevelMax" value="WARN" />
      </filter>
    </appender>
    <!--ERROR-->
    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="D:\Log\" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMMdd/'LogError_'yyyyMMdd&quot;.log&quot;" />
      <param name="StaticLogFileName" value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x]  - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR" />
        <param name="LevelMax" value="ERROR" />
      </filter>
    </appender>
  </log4net>

 <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
 </startup>
</configuration>
View Code

3.AssemblyInfo.cs文件中添加

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

4.创建日志记录组件

public static ILog mLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

mLog.Debug("this is a debug");
mLog.Info("this is info");
mLog.Error("it is error");

参考文章

 

posted @ 2018-04-28 21:03  summer91  阅读(137)  评论(0编辑  收藏  举报