log4net AdoNetAppender - SqlParameterCollection does not contain parameters
Asked Answered
F

2

9

In one of my projects I get this error:

System.IndexOutOfRangeException: SqlParameterCollection does not contain SqlParameter with ParameterName "@log_date".
в System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
в System.Data.Common.DbParameterCollection.System.Data.IDataParameterCollection.get_Item(String parameterName)
в log4net.Appender.AdoNetAppenderParameter.FormatValue(IDbCommand command, LoggingEvent loggingEvent)
в log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
в log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

Here is appender configuration (which works perfectly in others projects):

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="Data Source=*****;Initial Catalog=****;Integrated Security=SSPI" />
        <commandText value="INSERT INTO [LogsDB].[dbo].[Logs] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
        <parameter>
            <parameterName value="@log_date" />
            <dbType value="DateTime" />
            <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        .
        .
        .
        <parameter>
            <parameterName value="@exception" />
            <dbType value="String" />
            <size value="2000" />
            <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
    </appender>

If someone had this problem, I'll really appreciate for helping

Fabrianna answered 13/11, 2015 at 15:49 Comment(5)
Have you just upgraded to 1.2.14 as there seems to be an issueSomite
@Somite I just reproduced this. All other appenders seem to work.Redcoat
Really... in this new project I have the latest version of log4netFabrianna
will try to downgrade it and then post here the resultFabrianna
It looks like this has been resolved in 2.0.5.Luteolin
U
12

Install-Package Log4net -Version 2.0.3

Work for me(have same problem)

Urbannai answered 16/11, 2015 at 9:54 Comment(2)
Downgrading it to 2.0.3 solved the problem for now.Cryptoclastic
No need to downgrade now - Version 2.0.5 fixes the problem, just don't use 2.0.4!!Lethargic
E
-1

Solved it. Upgraded Log4Net to 2.04 ! Then ran the command supplied above ! No idea what I am doing. But I am now able to log to the database !

Eridanus answered 17/11, 2015 at 8:33 Comment(1)
If you mean the command Install-Package Log4net -Version 2.0.3 then you just downgraded to the 2.0.3 version. You can read about the bug here.Cardigan

© 2022 - 2024 — McMap. All rights reserved.