Log4Net 使用记录

每次搞这东西总要花不少时间来配置,这次记下,方便以后搬

 

一.添加程序程集Assembly

在使用log4Net类的namespace前加上:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace XX.Log.BLL
{
	//To do
}

如果是Asp.net应用程序

1. 在Properties/AssemblyInfo.cs文件中加上

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config" , Watch = true )] (独立配置文件)或

[assembly: log4net.Config.XmlConfigurator(Watch = true )] (配置在web.config文件中)

2.在Globall.cs的Application_Start中添加:

protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
        }

log4net.Config.XmlConfigurator.Configure有重载,如果是独立配置文件可以使用重载

 

3.添加配置文件,以下是使用是精简的

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="" />
      <appendToFile value="true" />
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="yyyyMMdd&quot;_log-file.txt&quot;"/>
      <param name="RollingStyle" value="Date"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>


    <appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="50" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=xxxx;initial catalog=xx;integrated security=false;persist security info=True;User ID=sa;Password=******" />
      <commandText value="INSERT INTO Logs ([LogTime],[AppCode],[LogLevel],[LogSource],[Message],[Description]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>

    </appender>
    <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->
    <root>
      <!--<level value="WARN"/>
      <level value="INFO"/>
      <level value="DEBUG"/>
      <level value="FINE"/>
      <appender-ref ref="ADONetAppender" />-->
    </root>

    <logger name="DBLogger">
      <level value="WARN"/>
      <level value="INFO"/>
      <level value="DEBUG"/>
      <level value="FINE"/>
      <appender-ref ref="ADONetAppender" />
    </logger>

    <logger name="FilelLoger">
      <level value="WARN"/>
      <level value="INFO"/>
      <level value="DEBUG"/>
      <level value="FINE"/>
      <appender-ref ref="FileAppender" />
    </logger>
  </log4net>

</configuration>

其中<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />可能会因为ADO版本不同会有所差异,Version=可能为1.0.0.3之类的

 

4. Log类包装

using System;
using System.Collections.Generic;
using System.Text;


using log4net;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace BLL
{
    public class LogBLL
    {
        static ILog loger;
        public ILog LogerInstance
        {
            get
            {
                if (loger == null)
                    loger = LogManager.GetLogger("DBLogger");
                return loger;
            }

        }


        protected void LogException(Exception ex)
        {
            LogerInstance.Error("",ex);
        }

        protected void LogInfo(string msg)
        {
            LogerInstance.Info(msg);
        }

    }
}

5 建表脚本

CREATE TABLE [dbo].[Log] (
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar] (255) NOT NULL,
    [Level] [varchar] (50) NOT NULL,
    [Logger] [varchar] (255) NOT NULL,
    [Message] [varchar] (4000) NOT NULL,
    [Exception] [varchar] (2000) NULL
)
    
posted @ 2012-03-07 21:44  徐某人  阅读(3007)  评论(1编辑  收藏  举报