首页 > 代码库 > log4net日志组件

log4net日志组件

一、什么是log4net组件

 

Log4net是基于.net开发的一款非常著名的记录日志开源组件。他最早是2001年7月由NeoWorks Limited启动的项目,基本的框架源于另外的一个非常著名的姐妹组件-log4j。Log4net记录日志的功能非常强大。它可以将日志分不同的等级,比不同的样式,将日志输出到不同的媒介(数据库、文本、控制台、邮件……)。 

 

二、log4net的核心内容 

 

Log4net 主要分为5个核心主键: Logger,Appender,Filter,Layout,Object Render  

Logger:  

主要用于记录日志的分类和控制日志的级别。它可以以多种格式输出日志信息,同时它也可以控制日志的输出级别。 

Log4net 分为如下几个级别: 

  FATAL  毁灭级别

  ERROR 错误级别

  WARN 警告级别

  INFO 消息级别

  DEBUG 调试级别

      这几种日志级别 FATAL> ERROR> WARN> INFO>DEBUG

      只有日志输出级别大于或等于配置的级别才能输出日志信息

      比如我的日志配置级别为INFO

      那么只有log.Info() log.Warn()log.Error() log.Fatal()才能输出日志信息,Debug方式就不能输出。

       Log4net中还有两种特殊的配置级别: ALL 允许所有的日志级别输出 OFF 拒绝所有的日志级别输出 

 

Appender: 

 

 

这里我们简单称之为 媒体。意思就是说日志的输出必须依赖于它,更确切的说以文本形式输出,还是控制台格式输出都必须要依靠它来控制。Log4net定义了多种媒体: 

(1) AdoNetAppender 将日志记录到数据库中。可以采用SQL和存储过程两种方式  

(2) AnsiColorTerminalAppender 在ANSI 窗口终端写下高亮度的日志事件  

(3) AspNetTraceAppender 能用asp.net中Trace的方式查看记录的日志 

(4) BufferingForwardingAppender 在输出到子Appenders之前先缓存日志事件  

(5) ConsoleAppender 将日志输出到控制台  

(6) EventLogAppender 将日志写到Windows Event Log  

(7) FileAppender 将日志写到文件中  

(8) MemoryAppender 将日志存到内存缓冲区  

(9) NetSendAppender 将日志输出到Windows Messenger service.这些日志信息将在用户终端的对话框中显示  

(10) RemoteSyslogAppender 通过UDP网络协议将日志写到Remote syslog service 

(11) RemotingAppender 通过.NET Remoting将日志写到远程接收端  

(12) RollingFileAppender 将日志以回滚文件的形式写到文件中  

(13) SmtpAppender 将日志写到邮件中  

(14) TraceAppender 将日志写到.NET trace 系统  

(15) UdpAppender 将日志connectionless UDP datagrams的形式送到远程宿主或以UdpClient的形式广播  

 

Filter:

 

Appender 是将日志以缺省值的方式输出,意思就是按照默认值的方式输出。而Filter 意思就是过滤器,使用Filter 可以将日志以不同格式来输出 

 

Layout: 

这个用于控制日志输出显示格式  

Object Render: 

用于按照用户自定义标准来输出日志  

 

三、代码演示跟着来 

 

0.在引用中添加log4net.dll 

 

1.新建一个配置文件(这里可以配置日志写入到sql 、 txt 、 邮件,以及设置日志级别等) 

 

<?xml version="1.0"?>
<configuration>

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
 <log4net debug="false">

  <!--按日期分割日志文件 一天一个-->
  <appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >
   <!--是否续写-->
   <param name="AppendToFile" value="http://www.mamicode.com/true" />
   <!--最小锁定模型以允许多个进程可以写入同一个文件-->
   <param name="LockingModel" value="http://www.mamicode.com/log4net.Appender.FileAppender.MinimalLock" />
   <param name="StaticLogFileName" value="http://www.mamicode.com/true" />
   <!--保存路径-->
   <param name="File" value="http://www.mamicode.com/F:/Log//" />
   <param name="DatePattern" value="http://www.mamicode.com/yyyy-MM-dd.LOG" />
   <param name="StaticLogFileName" value="http://www.mamicode.com/false" />
   <param name="RollingStyle" value="http://www.mamicode.com/Date" />
   <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="http://www.mamicode.com/时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
   </layout>
  </appender>

  <!--按日志容量分割日志文件 10KB一个-->
  <appender name="LogFileAppenderBySize" type="log4net.Appender.RollingFileAppender" >

   <!--是否续写-->
   <param name="AppendToFile" value="http://www.mamicode.com/true" />
   <!--最小锁定模型以允许多个进程可以写入同一个文件-->
   <param name="LockingModel" value="http://www.mamicode.com/log4net.Appender.FileAppender.MinimalLock" />

   <param name="StaticLogFileName" value="http://www.mamicode.com/true" />

   <!--按照文件的大小进行变换日志文件-->
   <param name="RollingStyle" value="http://www.mamicode.com/Size" />
   <param name="File" value="http://www.mamicode.com/LogData//log.txt" />
   <!--单个文件最大数量 好像只有在 按Size分割时有效-->
   <param name="MaximumFileSize" value="http://www.mamicode.com/10KB"/>
   <!--保留的log文件数量 超过此数量后 自动删除之前的   好像只有在 按Size分割时有效-->
   <param name="MaxSizeRollBackups" value="http://www.mamicode.com/2" />

   <param name="StaticLogFileName" value="http://www.mamicode.com/false" />
   <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="http://www.mamicode.com/发生时间:%d %n事件级别:%level %n相关类名:%c%n程序文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
   </layout>
  </appender>
  <!--记录日志到数据库-->
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
   <bufferSize value="http://www.mamicode.com/100" />
   <connectionType value="http://www.mamicode.com/System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   <connectionString value="http://www.mamicode.com/Data Source=LKYKJYXG-341C59/LKY;database = ITweb;Integrated security = true;" />
   <commandText value="http://www.mamicode.com/INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
   <parameter>
    <parameterName value="http://www.mamicode.com/@log_date" />
    <dbType value="http://www.mamicode.com/DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
   </parameter>
   <parameter>
    <parameterName value="http://www.mamicode.com/@thread" />
    <dbType value="http://www.mamicode.com/String" />
    <size value="http://www.mamicode.com/255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="http://www.mamicode.com/%thread" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="http://www.mamicode.com/@log_level" />
    <dbType value="http://www.mamicode.com/String" />
    <size value="http://www.mamicode.com/50" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="http://www.mamicode.com/%level" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="http://www.mamicode.com/@logger" />
    <dbType value="http://www.mamicode.com/String" />
    <size value="http://www.mamicode.com/255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="http://www.mamicode.com/%logger" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="http://www.mamicode.com/@message" />
    <dbType value="http://www.mamicode.com/String" />
    <size value="http://www.mamicode.com/4000" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="http://www.mamicode.com/%message" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="http://www.mamicode.com/@exception" />
    <dbType value="http://www.mamicode.com/String" />
    <size value="http://www.mamicode.com/2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
   </parameter>
  </appender>
  <root>
   <!--设置输出级别-->
   <level value="http://www.mamicode.com/INFO" />
   <!--启用按日期分割-->
   <appender-ref ref="LogFileAppenderByDate" />
   <!--启用按容量分割-->
   <!--<appender-ref ref="LogFileAppenderBySize" />-->
   <!--启用保存到数据库-->
   <appender-ref ref="AdoNetAppender" />
  </root>
 </log4net>
</configuration>

   

 

2.在Global.asax文件下写入代码 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("/log4nethh.config")));

        }

        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。

        }

    }
}

  

3.写入LogHelper类 

 

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

namespace WebApplication1
{

    /// <summary>
    /// 日志辅助类
    /// </summary>
    public class LogHelper
    {
        private static ILog log;
        private static LogHelper logHelper = null;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static ILog GetInstance()
        {
            logHelper = new LogHelper(null);

            return log;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public static ILog GetInstance(string configPath)
        {
            logHelper = new LogHelper(configPath);

            return log;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configPath"></param>
        private LogHelper(string configPath)
        {
            if (!string.IsNullOrEmpty(configPath))
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath));
            }
            else
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }


    }
}

  

 

4.使用Log4net的方法(下面的可以写为一个方法,哪里使用哪里调用。)

 

 

 Exception exp = Server.GetLastError();
            string strE = "\r\n堆栈:" + exp.StackTrace + "\r " + "Message:" + exp.Message + "\r 来源:" + exp.Source + "\r 来源:" + Request.Url;
            Response.Write(strE);
            // 在事件日志中记录异常信息           
            ILog log = LogHelper.GetInstance();
            log.Warn(strE);
            Server.ClearError();
            Server.Transfer("Error.aspx", false);

   

四、更详细的配置文件 

 

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetCoigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <!--MS SQL Server-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="http://www.mamicode.com/100" />
      <connectionType value="http://www.mamicode.com/System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="http://www.mamicode.com/data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
      <commandText value="http://www.mamicode.com/INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_date" />
        <dbType value="http://www.mamicode.com/DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@thread" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_level" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@logger" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@message" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@exception" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
    
    <!--MS Access-->
    <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
      <connectionString value="http://www.mamicode.com/Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/log/access.mdb;User Id=;Password=;" />
      <commandText value="http://www.mamicode.com/INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_date" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%date" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@thread" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_level" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@logger" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@message" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/1024" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message" />
        </layout>
      </parameter>
    </appender>
    
    
    <!--Oracle9i-->
    <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
      <connectionType value="http://www.mamicode.com/System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="http://www.mamicode.com/data source=[mydatabase];User ID=[user];Password=[password]" />
      <commandText value="http://www.mamicode.com/INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
      <bufferSize value="http://www.mamicode.com/128" />
      <parameter>
        <parameterName value="http://www.mamicode.com/:log_date" />
        <dbType value="http://www.mamicode.com/DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/:thread" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/:log_level" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/:logger" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/:message" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message" />
        </layout>
      </parameter>
    </appender>
    
    
    
    <!--IBM DB2-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="http://www.mamicode.com/100" />
      <connectionType value="http://www.mamicode.com/IBM.Data.DB2.DB2Connection,IBM.Data.DB2, Version=8.1.2.1" />
      <connectionString value="http://www.mamicode.com/server=192.168.0.0;database=dbuser;user Id=username;password=password;persist security info=true" />
      <commandText value="http://www.mamicode.com/INSERT INTO myschema.Log (Date,Thread,Level,Logger,Message,Exception) VALUES (@log_date,@thread,@log_level,@logger,@message,@exception)" />
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_date" />
        <dbType value="http://www.mamicode.com/DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@thread" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@log_level" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/500" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@logger" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@message" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@exception" />
        <dbType value="http://www.mamicode.com/String" />
        <size value="http://www.mamicode.com/2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
    
    
    <!--SQLite-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="http://www.mamicode.com/100" />
      <connectionType value="http://www.mamicode.com/Finisar.SQLite.SQLiteConnection, SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c" />
      <connectionString value="http://www.mamicode.com/Data Source=c://inetpub//wwwroot//logs//log4net.db;Version=3;" />
      <commandText value="http://www.mamicode.com/INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" />
      <parameter>
        <parameterName value="http://www.mamicode.com/@Date" />
        <dbType value="http://www.mamicode.com/DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@Level" />
        <dbType value="http://www.mamicode.com/String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@Logger" />
        <dbType value="http://www.mamicode.com/String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="http://www.mamicode.com/@Message" />
        <dbType value="http://www.mamicode.com/String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="http://www.mamicode.com/%message" />
        </layout>
      </parameter>
    </appender>
    
    
    <!--AspNetTraceAppender-->
    <appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--BufferingForwardingAppender-->
    <!--<appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
      <bufferSize value="http://www.mamicode.com/100"/>
      <appender-ref ref="ConsoleAppender" />
    </appender>-->
    <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
      <bufferSize value="http://www.mamicode.com/512" />
      <lossy value="http://www.mamicode.com/true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="http://www.mamicode.com/WARN"/>
      </evaluator>
      <appender-ref ref="ConsoleAppender" />
    </appender>
    
    
    <!--ColoredConsoleAppender-->
    <!--<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="http://www.mamicode.com/ERROR" />
        <foreColor value="http://www.mamicode.com/White" />
        <backColor value="http://www.mamicode.com/Red, HighIntensity" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>-->
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="http://www.mamicode.com/ERROR" />
        <foreColor value="http://www.mamicode.com/White" />
        <backColor value="http://www.mamicode.com/Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="http://www.mamicode.com/DEBUG" />
        <backColor value="http://www.mamicode.com/Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    
    <!--ConsoleAppender-->
    <!--<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>-->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <target value="http://www.mamicode.com/Console.Error" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    
    
    
    
    <!--EventLogAppender-->
    <!--<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>-->
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
      <applicationName value="http://www.mamicode.com/MyApp" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--FileAppender-->
    <!--<appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="http://www.mamicode.com/log-file.txt" />
      <appendToFile value="http://www.mamicode.com/true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="http://www.mamicode.com/${TMP}/log-file.txt" />
      <appendToFile value="http://www.mamicode.com/true" />
      <encoding value="http://www.mamicode.com/unicodeFFFE" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>-->
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="http://www.mamicode.com/${TMP}/log-file.txt" />
      <appendToFile value="http://www.mamicode.com/true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--ForwardingAppender-->
    <appender name="ForwardingAppender" type="log4net.Appender.ForwardingAppender" >
      <threshold value="http://www.mamicode.com/WARN"/>
      <appender-ref ref="ConsoleAppender" />
    </appender>
    
    
    <!--MemoryAppender-->
    <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
      <onlyFixPartialEventData value="http://www.mamicode.com/true" />
    </appender>
    
    
    <!--NetSendAppender-->
    <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
      <onlyFixPartialEventData value="http://www.mamicode.com/true" />
    </appender>
    
    
    <!--NetSendAppender-->
    <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender">
      <threshold value="http://www.mamicode.com/ERROR" />
      <server value="http://www.mamicode.com/SQUARE" />
      <recipient value="http://www.mamicode.com/nicko" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--OutputDebugStringAppender-->
    <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--RemotingAppender-->
    <!--<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
      <sink value="tcp://localhost:8085/LoggingSink" />
      <lossy value="http://www.mamicode.com/false" />
      <bufferSize value="http://www.mamicode.com/95" />
      <onlyFixPartialEventData value="http://www.mamicode.com/true" />
    </appender>-->
    <appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
      <sink value="tcp://localhost:8085/LoggingSink" />
      <lossy value="http://www.mamicode.com/true" />
      <bufferSize value="http://www.mamicode.com/200" />
      <onlyFixPartialEventData value="http://www.mamicode.com/true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="http://www.mamicode.com/ERROR"/>
      </evaluator>
    </appender>
    
    
    
    <!--RollingFileAppender-->
    <!--<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="http://www.mamicode.com/log.txt" />
      <appendToFile value="http://www.mamicode.com/true" />
      <rollingStyle value="http://www.mamicode.com/Size" />
      <maxSizeRollBackups value="http://www.mamicode.com/10" />
      <maximumFileSize value="http://www.mamicode.com/100KB" />
      <staticLogFileName value="http://www.mamicode.com/true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="http://www.mamicode.com/logfile" />
      <appendToFile value="http://www.mamicode.com/true" />
      <rollingStyle value="http://www.mamicode.com/Date" />
      <datePattern value="http://www.mamicode.com/yyyyMMdd-HHmm" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="http://www.mamicode.com/logfile" />
      <appendToFile value="http://www.mamicode.com/true" />
      <rollingStyle value="http://www.mamicode.com/Composite" />
      <datePattern value="http://www.mamicode.com/yyyyMMdd" />
      <maxSizeRollBackups value="http://www.mamicode.com/10" />
      <maximumFileSize value="http://www.mamicode.com/1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>-->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="http://www.mamicode.com/logfile.txt" />
      <appendToFile value="http://www.mamicode.com/false" />
      <rollingStyle value="http://www.mamicode.com/Size" />
      <maxSizeRollBackups value="http://www.mamicode.com/-1" />
      <maximumFileSize value="http://www.mamicode.com/50GB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--SmtpAppender-->
    <!--<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="http://www.mamicode.com/to@domain.com" />
      <from value="http://www.mamicode.com/from@domain.com" />
      <subject value="http://www.mamicode.com/test logging message" />
      <smtpHost value="http://www.mamicode.com/SMTPServer.domain.com" />
      <bufferSize value="http://www.mamicode.com/512" />
      <lossy value="http://www.mamicode.com/true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="http://www.mamicode.com/WARN"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="http://www.mamicode.com/to@domain.com" />
      <from value="http://www.mamicode.com/from@domain.com" />
      <subject value="http://www.mamicode.com/test logging message" />
      <smtpHost value="http://www.mamicode.com/SMTPServer.domain.com" />
      <bufferSize value="http://www.mamicode.com/512" />
      <lossy value="http://www.mamicode.com/false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
      </layout>
    </appender>-->
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
      <to value="http://www.mamicode.com/to@domain.com" />
      <from value="http://www.mamicode.com/from@domain.com" />
      <subject value="http://www.mamicode.com/test logging message" />
      <smtpHost value="http://www.mamicode.com/SMTPServer.domain.com" />
      <bufferSize value="http://www.mamicode.com/512" />
      <lossy value="http://www.mamicode.com/false" />
      <evaluator type="log4net.Core.LevelEvaluator,log4net">
        <threshold value="http://www.mamicode.com/WARN" />
      </evaluator>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionPattern value="http://www.mamicode.com/%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
      </layout>
    </appender>
    
    
    <!--SmtpPickupDirAppender-->
    <appender name="SmtpPickupDirAppender" type="log4net.Appender.SmtpPickupDirAppender">
      <to value="http://www.mamicode.com/to@domain.com" />
      <from value="http://www.mamicode.com/from@domain.com" />
      <subject value="http://www.mamicode.com/test logging message" />
      <pickupDir value="http://www.mamicode.com/C:/SmtpPickup" />
      <bufferSize value="http://www.mamicode.com/512" />
      <lossy value="http://www.mamicode.com/true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="http://www.mamicode.com/WARN"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
      </layout>
    </appender>
    
    
    <!--TraceAppender-->
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="http://www.mamicode.com/%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    
    
    <!--UdpAppender-->
    <appender name="UdpAppender" type="log4net.Appender.UdpAppender">
      <localPort value="http://www.mamicode.com/8080" />
      <remoteAddress value="http://www.mamicode.com/224.0.0.1" />
      <remotePort value="http://www.mamicode.com/8080" />
      <layout type="log4net.Layout.PatternLayout, log4net">
        <conversionPattern value="http://www.mamicode.com/%-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

log4net日志组件