log4net AdoNetAppender缺少默认参数

时间:2017-11-23 13:58:04

标签: .net log4net

尝试将log4net AdoNetAppender与示例/默认配置一起使用我得到了一个奇怪的参数错误

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
System.IndexOutOfRangeException: An SqlParameter with ParameterName '@log_date' is not contained by this SqlParameterCollection.

我没有看到参数声明有什么问题

<parameter>
    <parameterName value="@log_date"/>
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>

但是,试图删除参数,它只是下一个以同样的方式失败的参数,我试图改变连接字符串,认为可能存在潜在的错误我&#39;我没有看到,也没有运气。

log4net调试日志甚至清楚地说明了

 log4net: Setting Collection Property [AddParameter] to object [log4net.Appender.AdoNetAppenderParameter] 

对于每个参数,所以我真的不知道发生了什么

Bellow是我对log4net

的完整配置
<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="DBAppender" />
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="DebugAppender" />
  </root>

  <appender name="DBAppender" type="log4net.Appender.AdoNetAppender">
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <connectionStringName value="CustomerDB" />
    <bufferSize value="1" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
    <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>

  <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
    <immediateFlush value="true" />
    <layout type="log4net.Layout.SimpleLayout" />
  </appender>
</log4net>

1 个答案:

答案 0 :(得分:2)

您需要更新到log4net 2.0.5,2.0.4 AdoNetAppender已损坏。