Log4net记录日志到Oracle 11g

试了多次,当前的Log4net都不支持记录日志到Oracle 11g数据库,于是各种搜索。
1. 下载log4net源代码进行改写
    1.1 添加对Oracle.DataAccess的引用
    1.2 添加OracleAppender类  点击此处下载
    也可以点击此处下载编译好的 log4net.dll
2. 新建Web项目,添加对log4net.dll和Oracle.DataAccess.dll的引用,并进行Web.config配置
    2.1 增加section
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

       2.2 增加log4net配置

<log4net>

    <appender name="OracleAppender" type="log4net.Appender.OracleAppender">

      <bufferSize value="1" />

      <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=4.113.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

      <connectionString value="DATA SOURCE=数据库TNSNAME;USER ID=用户名;PASSWORD=密码"/>

      <commandText value="INSERT INTO LOG4NET (LOG_ID, LOG_DATE, LOG_LEVEL, LOG_IDENTITY, LOG_MESSAGE, LOG_EXCEPTION, LOG_LOGGER, LOG_SOURCE) VALUES (LOG4NET_SEQ.nextval, :log_date, :log_level, :log_identity, :log_message, :log_exception, :logger, :source)" />

      <parameter>

        <parameterName value=":log_date" />

        <dbType value="DateTime" />

        <layout type="log4net.Layout.RawTimeStampLayout">

          <conversionPattern value="%d{yyyy/MM/dd HH:mm:ss}" />

        </layout>

      </parameter>

      <parameter>

        <parameterName value=":log_level" />

        <dbType value="String" />

        <size value="10" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%level" />

        </layout>

      </parameter>

      <parameter>

        <parameterName value=":log_identity" />

        <dbType value="String" />

        <size value="100" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%identity" />

        </layout>

      </parameter>

      <parameter>

        <parameterName value=":log_message" />

        <dbType value="String" />

        <size value="4000" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%m" />

        </layout>

      </parameter>

      <parameter>

        <parameterName value=":log_exception" />

        <dbType value="String" />

        <size value="4000" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%exception" />

        </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=":source" />

        <dbType value="String" />

        <size value="1000" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%file:%line" />

        </layout>

      </parameter>

    </appender>

    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">

      <param name="File" value="Log\\LogInfo\\" />

      <param name="AppendToFile" value="true" />

      <param name="MaxFileSize" value="10240" />

      <param name="MaxSizeRollBackups" value="100" />

      <param name="StaticLogFileName" value="false" />

      <param name="DatePattern" value="yyyyMMdd&quot;.log&quot;" />

      <param name="RollingStyle" value="Date" />

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%n日志时间:%d [%t] %n日志级别:%-5p %n日 志 类:%c [%x] %n%m %n" />

      </layout>

    </appender>

    <logger name="loginfo">

      <level value="INFO" />

      <appender-ref ref="InfoAppender" />

    </logger>

    <root>

      <level value="All"/>

      <appender-ref ref="OracleAppender"/>

    </root>

  </log4net>
      2.3 增加dependentAssembly配置
<dependentAssembly>

        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />

      </dependentAssembly>
3. 配置代码记录LOG。(global.asax文件中的Application_Start方法中添加如下代码)
log4net.Config.XmlConfigurator.Configure();

            ILog log = LogManager.GetLogger("loginfo");

            log.Info("Application Start");

   4 配置Oracle数据库

    4.1 建表

create table LOG4NET

(

  log_id        NUMBER not null,

  log_date      TIMESTAMP(6),

  log_level     VARCHAR2(255),

  log_identity  VARCHAR2(255),

  log_message   VARCHAR2(4000),

  log_exception VARCHAR2(4000),

  log_logger    VARCHAR2(255),

  log_source    VARCHAR2(1000)

)

    4.2 建序列

create sequence LOG4NET_SEQ

minvalue 1

maxvalue 999999999999999999999999999

start with 53

increment by 1

nocache;

   5. 最终效果

image

你可能感兴趣的:(oracle 11g)