如何从log4net中的存储过程插入数据库?

时间:2010-08-11 10:55:50

标签: log4net adonetappender threadcontext

我必须记录这样的线程上下文属性:

string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "log4netconfig.xml";
FileInfo finfo = new FileInfo(logFilePath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);
ILog logger = LogManager.GetLogger("Exception.Logging");

log4net.ThreadContext.Properties["MESSAGE"] = exception.Message;
log4net.ThreadContext.Properties["MODULE"] = "module1";
log4net.ThreadContext.Properties["COMPONENT"] = "component1";
logger.Debug("test");

,配置文件为:

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

  <log4net>
    <logger name="Exception.Logging" level="Debug">
      <appender-ref ref="AdoNetAppender"/>
    </logger>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <connectionString value="Data Source=xe;User ID=test;Password=test;" />
      <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <bufferSize value="10000"/>
      <commandText value="Log_Exception_Pkg.Insert_Log" />
      <commandType value="StoredProcedure" />

      <parameter>
        <parameterName value="@p_Error_Message" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{MESSAGE}"/>
        </layout>
      </parameter>

      <parameter>
        <parameterName value="@p_Module" />
        <dbType value="String" />
        <size value="225" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{MODULE}"/>
        </layout>
      </parameter>

      <parameter>
        <parameterName value="@p_Component" />
        <dbType value="String" />
        <size value="225" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{COMPONENT}"/>
        </layout>
      </parameter>
    </appender>
  </log4net>
</configuration>

但它没有将它们插入数据库中。我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:2)

简单回答:因为这是一个Oracle连接,所以从参数名中删除@符号就可以了。

示例:

<parameter>
    <parameterName value="@p_Module" />
    <dbType value="String" />
    <size value="225" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{MODULE}"/>
    </layout>
</parameter>

答案 1 :(得分:0)

当log4net表处于不同的架构时,我遇到了类似的问题。 (如果它们处于不同的模式中,它肯定也适用于存储过程或包)

不要忘记在表或存储过程之前键入您的模式名称。

示例:

    <log4net>    
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
        <connectionString ..../>
        <commandText value="INSERT INTO schemaName.LOG4NET (LOG_DATE, LOG_LEVEL, LOG_IDENTITY, LOG_MESSAGE, LOG_EXCEPTION) VALUES (:log_date, :log_level, :log_identity, :log_message, :log_exception)" />
....