1

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

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>

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

4

2 回答 2

2

简单的回答:因为这是一个 Oracle 连接,所以从参数名称中删除 @ 符号,它将起作用。

样本:

<parameter>
    <parameterName value="@p_Module" />
    <dbType value="String" />
    <size value="225" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{MODULE}"/>
    </layout>
</parameter>
于 2011-01-04T16:50:46.703 回答
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)" />
....
于 2013-12-11T13:51:38.490 回答