.Net中Log4Net的使用

2018-08-23

 

.Net中Log4Net的使用

 

一、log4net 分类型记录日志存放多个日志文件

1、在webconfig里添加配置

  1.1 在<configSections> 中添加节点

 
<configuration>
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections>
</configuration>

  1.2 添加log4net配置section

<log4net>
    <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="FATAL" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <File value="Logs\fatal.log" />
      <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="errorAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="ERROR" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <File value="Logs\err.log" />
      <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="warnAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="WARN" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <File value="Logs\warn.log" />
      <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" />
      <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" />  <!--这里可以配置日志存放位置-->
      <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" />
      <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="fatalAppender" />
      <appender-ref ref="errorAppender" />
      <appender-ref ref="warnAppender" />
      <appender-ref ref="infoAppender" />
      <appender-ref ref="debugAppender" />
    </root>
  </log4net>

2、在Global.asax.cs里添加

 log4net.Config.XmlConfigurator.Configure();

3、添加 helper类

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestLog4Net
{
    public class log4netHelper
    {
        public static void Fatal(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Fatal(message);
            else
                log.Fatal(message, exception);
        }

        public static void Error(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Error(message);
            else
                log.Error(message, exception);
        }

        public static void Warn(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Warn(message);
            else
                log.Warn(message, exception);
        }

        public static void Info(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Info(message);
            else
                log.Info(message, exception);
        }

        public static void Debug(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Debug(message);
            else
                log.Debug(message, exception);
        }

    }
}

4、代码里使用

   ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//获取一个日志记录器
            log4netHelper.Fatal(this.GetType(), "Fatal");                           //致命错误
            log4netHelper.Error(this.GetType(), "Error", new Exception("error"));   //错误
            log4netHelper.Warn(this.GetType(), "Warn");                             //禁告
            log4netHelper.Info(this.GetType(), "Info");                             //信息
            log4netHelper.Debug(this.GetType(), "Debug");                           //调试

FATAL(致命错误):记录系统中出现的能使用系统完全失去功能,服务停止,系统崩溃等使系统无法继续运行下去的错误。例如,数据库无法连接,系统出现死循环。

ERROR(一般错误):记录系统中出现的导致系统不稳定,部分功能出现混乱或部分功能失效一类的错误。例如,数据字段为空,数据操作不可完成,操作出现异常等。

WARN(警告):记录系统中不影响系统继续运行,但不符合系统运行正常条件,有可能引起系统错误的信息。例如,记录内容为空,数据内容不正确等。

INFO(一般信息):记录系统运行中应该让用户知道的基本信息。

DEBUG (调试信息):记录系统用于调试的一切信息,内容或者是一些关键数据内容的输出。

 

二、存放单个日志

 

 

1、在以上基础上 配置修改

  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </root>
    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="log" />
    </logger>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <!--<param name="File" value="App_Data/" />-->
      <File value="Logs\log" />  <!--日志文件位置和文件名-->
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <!--<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />-->
      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" /><!--在文件名后面加内容 比如 log名变为log20180808-->
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>

2、代码使用

ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//获取一个日志记录器            
log.Fatal("info");
log.Error(
"Error");
log.Warn(
"Warn");
log.Info(
"info");
log.Debug(
"Debug");

 

posted @ 2018-08-23 14:33  i_mengli  阅读(763)  评论(1编辑  收藏  举报