背景:
阅读新闻

The Log4net log records to the Oracle 11g

  作者: 今日评论: [字体: ]

Tried many times, the current Log4net does not support the log to Oracle 11g database, so a variety of search. 
1 download the log4net source code is rewritten
    1.1 add a reference to Oracle.DataAccess
    1.2 add OracleAppender  Click here to download
    Can also click here to download the compiled log4net.dll
2 new Web project, add a reference to the log4net.dll and Oracle.DataAccess.dll, and the Web.config configuration
    The 2.1 increase in section
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

       The 2.2 increase in log4net configuration

<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= database TNSNAME; USER ID= user name PASSWORD= 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 log time:%d [%t]%n log level:%-5p%n Chronicles: %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 increaseThe dependentAssembly configuration
<dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
The 3 configuration code record LOG. (add the following code in the global.asax file in the Application_Start method)
log4net.Config.XmlConfigurator.Configure();
            ILog log = LogManager.GetLogger("loginfo");
            log.Info("Application Start");

   The 4 configuration of Oracle database

    4.1 tables

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 construction sequence

create sequence LOG4NET_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 53
increment by 1
nocache;

   5 the final effect

image


http://odasusu.hatenablog.com/entry/2015/10/16/102112

/// <summary>
/// AdoNetAppenderの設定
/// </summary>
private void updateLog4NetAdoNetAppender(string serverName, string userName, string password, string database)
{
    Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
    if (hierarchy != null && hierarchy.Configured)
    {
        foreach (IAppender appender in hierarchy.GetAppenders())
        {
            if (appender is AdoNetAppender)
            {
                var adoNetAppender = (AdoNetAppender)appender;
                adoNetAppender.ConnectionString = string.Format("data source={0};integrated security=false;persist security info=True;User ID={1};Password={2};initial catalog={3}"
                    , serverName
                    , userName
                    , password
                    , databaseName);
                adoNetAppender.ActivateOptions();
            }
        }
    }
}

/// <summary>
/// カスタム項目の設定
/// </summary>
private void setLog4netProperties(string test)
{
        log4net.ThreadContext.Properties["test"] = test;
}


来源:http://www.programering.com/a/MTM5ADMwATc.html
录入日期:[2016/07/01 15:27:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论表单加载中...