首页 > 代码库 > .Net Log4Net配置多文件日志记录

.Net Log4Net配置多文件日志记录

其他配置详情在网上都可以找到,但是很多看着都晕,本人就记录一下如何使用:

  1、按不同级别(官方说明)可记录的日志级别有:

    Info、Warn、Error、Debug

  2、可以按着四个配置四个输出日志路径和配置文件的配置节点

  可以在filter节点中配置value来区别记录日志的级别

  例如:

 

<appender name="Errorlog" type="log4net.Appender.RollingFileAppender">      <!--定义文件存放位置-->      <file value=http://www.mamicode.com/"Errorlog\\"/>      <appendToFile value=http://www.mamicode.com/"true"/>      <rollingStyle value=http://www.mamicode.com/"Date"/>      <datePattern value=http://www.mamicode.com/"yyyyMMdd‘.txt‘"/>      <staticLogFileName value=http://www.mamicode.com/"false"/>      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明        输出格式        样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info        <conversionPattern value=http://www.mamicode.com/"%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->        <conversionPattern value=http://www.mamicode.com/"%date 描述:%message%n"/>      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value=http://www.mamicode.com/"ERROR"/>        <param name="LevelMax" value=http://www.mamicode.com/"ERROR"/>      </filter>    </appender>

 

然后可以根据自己需要配置不同的输出路径,注意filter里的param节点的value必须大写并只能是四种节点的一种value

最终可以在logger节点中配置指向appender

<logger name="myLogger">
<level value="http://www.mamicode.com/ALL"/>
<appender-ref ref="Errorlog"/>
<appender-ref ref="Runlog"/>
<appender-ref ref="Warnlog"/>
<appender-ref ref="Debuglog"/>
<!--<appender-ref ref="ErrorLoging" />-->
</logger>

最后附上配置文件以及LogHelper类:

<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>  </configSections>  <log4net>    <!--定义输出到文件中-->    <appender name="Errorlog" type="log4net.Appender.RollingFileAppender">      <!--定义文件存放位置-->      <file value=http://www.mamicode.com/"Errorlog\\"/>      <appendToFile value=http://www.mamicode.com/"true"/>      <rollingStyle value=http://www.mamicode.com/"Date"/>      <datePattern value=http://www.mamicode.com/"yyyyMMdd‘.txt‘"/>      <staticLogFileName value=http://www.mamicode.com/"false"/>      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明        输出格式        样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info        <conversionPattern value=http://www.mamicode.com/"%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->        <conversionPattern value=http://www.mamicode.com/"%date 描述:%message%n"/>      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value=http://www.mamicode.com/"ERROR"/>        <param name="LevelMax" value=http://www.mamicode.com/"ERROR"/>      </filter>    </appender>    <appender name="Runlog" type="log4net.Appender.RollingFileAppender">      <!--定义文件存放位置-->      <file value=http://www.mamicode.com/"Runlog\\"/>      <appendToFile value=http://www.mamicode.com/"true"/>      <rollingStyle value=http://www.mamicode.com/"Date"/>      <datePattern value=http://www.mamicode.com/"yyyyMMdd‘.txt‘"/>      <staticLogFileName value=http://www.mamicode.com/"false"/>      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明        输出格式        样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info        <conversionPattern value=http://www.mamicode.com/"%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->        <conversionPattern value=http://www.mamicode.com/"%date 描述:%message%n"/>      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value=http://www.mamicode.com/"INFO"/>        <param name="LevelMax" value=http://www.mamicode.com/"INFO"/>      </filter>    </appender>    <appender name="Warnlog" type="log4net.Appender.RollingFileAppender">      <!--定义文件存放位置-->      <file value=http://www.mamicode.com/"Warnlog\\"/>      <appendToFile value=http://www.mamicode.com/"true"/>      <rollingStyle value=http://www.mamicode.com/"Date"/>      <datePattern value=http://www.mamicode.com/"yyyyMMdd‘.txt‘"/>      <staticLogFileName value=http://www.mamicode.com/"false"/>      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明        输出格式        样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info        <conversionPattern value=http://www.mamicode.com/"%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->        <conversionPattern value=http://www.mamicode.com/"%date 描述:%message%n"/>      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value=http://www.mamicode.com/"WARN"/>        <param name="LevelMax" value=http://www.mamicode.com/"WARN"/>      </filter>    </appender>    <appender name="Debuglog" type="log4net.Appender.RollingFileAppender">      <!--定义文件存放位置-->      <file value=http://www.mamicode.com/"Debuglog\\"/>      <appendToFile value=http://www.mamicode.com/"true"/>      <rollingStyle value=http://www.mamicode.com/"Date"/>      <datePattern value=http://www.mamicode.com/"yyyyMMdd‘.txt‘"/>      <staticLogFileName value=http://www.mamicode.com/"false"/>      <param name="MaxSizeRollBackups" value=http://www.mamicode.com/"100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明        输出格式        样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info        <conversionPattern value=http://www.mamicode.com/"%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->        <conversionPattern value=http://www.mamicode.com/"%date 描述:%message%n"/>      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value=http://www.mamicode.com/"DEBUG"/>        <param name="LevelMax" value=http://www.mamicode.com/"DEBUG"/>      </filter>    </appender>    <logger name="myLogger">      <level value=http://www.mamicode.com/"ALL"/>      <appender-ref ref="Errorlog"/>      <appender-ref ref="Runlog"/>      <appender-ref ref="Warnlog"/>      <appender-ref ref="Debuglog"/>      <!--<appender-ref ref="ErrorLoging" />-->    </logger>    <!--<root>      <level value=http://www.mamicode.com/"ALL"/>      -->    <!--文件形式记录日志-->    <!--      <appender-ref ref="SuoerServerLog"/>      <appender-ref ref="PDFWatcherLog"/>    </root>-->  </log4net>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  </startup></configuration>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]namespace DotNet.Utilities.Log{    public class LogHelper    {        //配置loger指向        static log4net.ILog log = log4net.LogManager.GetLogger("myLogger");        /// <summary>        /// 输出日志到Log4Net        /// </summary>        /// <param name="t"></param>        /// <param name="msg"></param>        #region Error节点        public static void WriteErrorLog(string msg)        {            //log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Error(msg);        }        #endregion        #region Info节点        public static void WriteInfoLog(string msg)        {            //log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Info(msg);        }        #endregion         #region Debug节点        public static void WriteDebugLog(string msg)        {            //log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Debug(msg);        }        #endregion         #region Warn节点        public static void WriteWarnLog(string msg)        {            //log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Warn(msg);        }        #endregion    }}

 

.Net Log4Net配置多文件日志记录