1

I am getting the following error in the log4net error log:

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
System.IndexOutOfRangeException: An SqlParameter with ParameterName '@logId' is not contained by this SqlParameterCollection.
   at System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
   at System.Data.Common.DbParameterCollection.System.Data.IDataParameterCollection.get_Item(String parameterName)
   at log4net.Appender.AdoNetAppenderParameter.FormatValue(IDbCommand command, LoggingEvent loggingEvent)
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

Here is the code from the web.config:

<commandText value="INSERT INTO WebTeamAdminAppLog ([ID],[Date],[Application],[Server],[ProcessId],[Login],[Thread],[Level],[Logger],[Message],[Exception],[SessionId]) VALUES (@logId,@log_date,@app,@server,@processId,@login,@thread,@log_level,@logger,@message,@exception,@sessionid)" />
  <parameter>
    <parameterName value="@logId" />
    <dbType value="Guid" />
    <layout type="log4net.Layout.RawPropertyLayout">
      <key value="logId" />
    </layout>
  </parameter>

Not sure what the problem is since this code is from another project that inserts logs into the database just fine. It is the same database, just a different table. The version of log4net in the other application is 1.2.13.0. Also, when I step through the debugger, the logId value seems to be getting set correctly.

ADH
  • 2,971
  • 6
  • 34
  • 53

2 Answers2

1

I just found the following SO post. Seems to be the exact problem I am having: log4net AdoNetAppender - SqlParameterCollection does not contain parameters

Downgrading to 1.2.13 fixed the problem for me.

Community
  • 1
  • 1
ADH
  • 2,971
  • 6
  • 34
  • 53
  • Upgrading to 1.2.15 also works. This issue is isolated to1.2.14. Reference comment from Gonzalo Diaz above. – Jim Aug 30 '17 at 05:45
0

Check your code if you have something like this:

 Guid id = Guid.NewGuid();
 var loggingEvent = new LoggingEvent(.....)
 loggingEvent.Properties["logId"] = id;
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Gonzalo Diaz
  • 139
  • 1
  • 8
  • I have something very similar to that: Guid guid = Guid.NewGuid(); log4net.ThreadContext.Properties["logId"] = guid; – ADH Nov 24 '15 at 19:41
  • 1
    Try updating to version 1.2.15. It seems to be a problem with version 1.2.14. Check: https://issues.apache.org/jira/browse/LOG4NET-489?jql=text%20~%20%22An%20SqlParameter%20with%20ParameterName%20is%20not%20contained%20by%20this%20SqlParameterCollection%22 – Gonzalo Diaz Nov 24 '15 at 19:56