log4net的配置及使用

网上查了有很多种写法和配置,结果百度出来都是几种方法混合写法,拷在一起结果还不能正常运行。因此把自己做成功的代码写上来做个备份。

运行环境:log4net 2.03版本,.net 4.5

大体步骤为:

    1:新建一个log4net.config配置文件
    2:创建一个Logger静态类写日志

具体步骤为:

一:在App或者Web根目录创建一个log4net.config文件,完整内容如下:

        一定要注意这就是完整的配置文件内容,有些文章里面说要把内容拷在<configuration>节点下,结果不能正常写日志

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="ERROR" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="Logs\err.log" />
    <encoding value="utf-8"/>
    <preserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="Logs\info.log" />
    <encoding value="utf-8"/>
    <preserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="DEBUG" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="Logs\debug.log" />
    <encoding value="utf-8"/>
    <preserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="Logs\perf.log" />
    <encoding value="utf-8"/>
    <preserveLogFileNameExtension value="true" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %logger - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="errorAppender" />
    <appender-ref ref="infoAppender" />
    <appender-ref ref="debugAppender" />
  </root>
  <logger name="Performance" additivity="false">
    <level value="ALL" />
    <appender-ref ref="perfAppender" />
  </logger>
</log4net>

基本能看懂,这个配置方法定义了几个过滤器,把不同级别的日志写在不同名称的文件中,也可以修改配置写到一个日志文件中。

 

二:创建日志静态类

    /// <summary>
    /// 框架日志类
    /// </summary>
    public class WRLogger
    {

        static ILog _logger = null;
        /// <summary>
        /// 默认日志类
        /// </summary>
        static ILog logger
        {
            get
            {
                if (_logger == null)
                {
                    log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
                    _logger = LogManager.GetLogger("Performance");
                }
                return _logger;
            }
        }
        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(string msg)
        {

            logger.Info(msg);
        }
        /// <summary>
        /// 记录调试日志,主要用于SQL或一些方法调用
        /// </summary>
        /// <param name="msg"></param>
        public static void Debug(string msg) { 
            logger.Debug(msg);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg) {
            logger.Error(msg);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex"></param>
        public static void Error(Exception ex)
        {
            logger.Error(ex);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="msg"></param>
        public static void Error(Exception ex,string msg)
        {
            logger.Error( msg+" "+ex.Message, ex);
        }

    }

其中:

log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
这句话特别关键,载入配置文件,这种方式载入,
1:则不需要在“AssemblyInfo.cs”中写配置
2:也不需要在app.config或者web.config中写任何关于log4net的配置信息

这样就可以直接使用了。
posted @ 2019-10-23 04:19  coolsundy  阅读(626)  评论(0编辑  收藏  举报