Log4net日志记录组件的使用步骤详解和下载

时间:2021-12-31 23:57:47

第一步:

添加并应用Log4net.dll。然后在Web.config文件中添加下面的配置局

 

复制代码代码如下:


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

 

 


第二步:

 

新建Log4Net.config的配置文件,并在其中添加下面的配置信息:

 

复制代码代码如下:


<?xml version="1.0" encoding="UTF-8"?>
<log4net debug="false">

 

  <!--按日期分割日志文件 一天一个-->
  <appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >

    <!--是否续写-->
    <param name="AppendToFile" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
    <!--最小锁定模型以允许多个进程可以写入同一个文件-->
    <param name="LockingModel" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"log4net.Appender.FileAppender.MinimalLock" />
    <param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
    <!--保存路径-->
    <param name="File" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"d:\Log\\" />
    <param name="DatePattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"yyyy-MM-dd.LOG" />
    <param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"false" />
    <param name="RollingStyle" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"Date" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"时间:%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.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
    <!--最小锁定模型以允许多个进程可以写入同一个文件-->
    <param name="LockingModel" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"log4net.Appender.FileAppender.MinimalLock" />

    <param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />

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

    <param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"false" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"发生时间:%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.cnblogs.com/huanghai223/archive/2012/02/21/"1" />
    <!--缓冲大小-->
    <connectionType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"Data Source=.\SQLEXPRESS;Initial Catalog=CVDS;User ID=sa;Password=sasa" />
    <commandText value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"INSERT INTO Log([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@log_date" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@thread" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
      <size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%thread" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@log_level" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
      <size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"50" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%level" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@logger" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
      <size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@message" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
      <size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"4000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%message" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@exception" />
      <dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
      <size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"2000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>

  <root>
    <level value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"INFO" />
    <!--启用按日期分割-->
    <appender-ref ref="LogFileAppenderByDate" />
    <!--启用按容量分割-->
    <!--<appender-ref ref="LogFileAppenderBySize" />-->
    <!--启用保存到数据库-->
    <!--<appender-ref ref="AdoNetAppender" />-->
  </root>

</log4net>

 

 

第三步:

在Global.asax文件中的Application_Start事件中添加如下代码:

 

复制代码代码如下:


protected void Application_Start(object sender, EventArgs e)
  {
       // Code that runs on application startup
      log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("Log4Net.config")));  
  }

 

 


第四步:调用LOG4net 写日志

 

 

复制代码代码如下:


protected void Button2_Click(object sender, EventArgs e)
        {
                     ILog logs = LogManager.GetLogger(typeof(TEST));

 

           logs.Fatal("Excption:这里就是要提示的LOG信息");
        }

 

 

也可以通过写一个LogHelper.cs类进行封装:

 

复制代码代码如下:


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

 

namespace SBIT.Web.Class
{
    /// <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);
            }
        }

    }
}

 

 

通过封装类写日志的方法如下

复制代码代码如下:

 private static readonly ILog logs = LogHelper.GetInstance(); //LogManager.GetLogger(typeof(TEST));
        protected void Button2_Click(object sender, EventArgs e)
        {
                logs.Fatal("Excption:这里就是要提示的LOG信息");
        }