开源组件应用系列:Log4net之基本配置(收藏)

 log4net是大家在日常开发中非常用的一个开源组件,下载地址:http://logging.apache.org/log4net/download_log4net.cgi

Appdender常用配置

    下面介绍的最常用的配置中的几种:

    1.ConoleAppender

 <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/>
      </layout>
    </appender>

  2.FileAppender

 <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="log.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{yyyy/MM/dd HH:mm:ss} [current thread id:%thread] 
                           %-5level %logger [%property] - %message%newline"/>
      </layout>
    </appender>

  3.RollingFileAppender

   这个是记录系统日志中最常用的。

 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <file value="log/log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <StaticLogFileName value="false"/>
      <CountDirection value="1"/>
      <datePattern value="yyyyMMdd&quot;&quot;" />
      <maxSizeRollBackups value="50" />
      <maximumFileSize value="1MB" />
      <Encoding value="UTF-8" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{yyyy/MM/dd HH:mm:ss} [%thread] %-5level %logger 
                           [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

4.AdoNetAppender

使用SQLServer连接

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <!--specified log table definition  -->
      <![CDATA[
      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
)
      ]]>
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,
                      Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=.;Initial Catalog=MyTest;User ID=sa;Password=password" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 
                   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>

oracle数据库中,使用ODP.NET连接:

 

<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
      <!--specified log table definition  -->
      <![CDATA[
      create table log (
      Datetime timestamp(3),
      Thread varchar2(255),
      Log_Level varchar2(255),
      Logger varchar2(255),
      Message varchar2(4000)
      );
      ]]>

      <!--<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient,
      Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />-->
      <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, 
                      Version=10.2.0.100, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      <connectionString value="Data Source=databasename;User ID=username;Password=password;" />
      <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) 
                   VALUES (:log_date, :thread, :log_level, :logger, :message)" />
      <bufferSize value="128" />
      <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>
    </appender>

  目前开发的系统中,我比较倾向于使用ODP.NET组件来访问oracle,它要比微软的提供OracleClient效率要高,而且操作很方便。

Logger配置

  <root>
      <level value="ERROR" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
    <logger name="RollFileLog">
      <level value="INFO"/>
      <appender-ref ref="RollingLogFileAppender"/>
      <appender-ref ref="ConsoleAppender" />
    </logger>
    <logger name="SQLDBLog">
      <level value="INFO"/>
      <appender-ref ref="AdoNetAppender"/>
      <appender-ref ref="ConsoleAppender" />
    </logger>
    <logger name="Oracle10gLog">
      <level value="INFO"/>
      <appender-ref ref="AdoNetAppender_Oracle"/>
      <appender-ref ref="ConsoleAppender" />
    </logger>

  需要注意的是,root节点中配置的level级别,可以被继承到logger中。

使用log4net

使用logtnet是,需要首先加载配置文件,然后在实例化需要制定使用的logger。

加载配置文件的方式一般使用XmlConfigurator.Configure来加载,在assembly.cs文件中加载可以最为方便,C/S、B/S系统均可以使用。

//[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"..\..\Config\Log4net.config", Watch = true)]
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

参数Watch=true表示日志组件监视配置文件发生更改。

//log4net.Config.XmlConfigurator.Configure();
            //log4net.ILog log = log4net.LogManager.GetLogger("RollFileLog");
            //log4net.ILog log = log4net.LogManager.GetLogger("SQLDBLog");
            //log4net.ILog log = log4net.LogManager.GetLogger("Oracle10gLog");
            //XmlConfigurator.Configure(new System.IO.FileInfo(@"..\..\Config\Log4net.config"));
            ILog log = LogManager.GetLogger("MySQLDB");
            log.Info("this is my log file test.");
            log.Error("this is my log file test->error.");
          
            log.Fatal("test");
posted @ 2012-02-14 09:50  Ruiky  阅读(929)  评论(0编辑  收藏  举报