在向MySQL数据库写异常时发生Log4Net异常

时间:2016-03-10 11:35:56

标签: c# mysql database logging log4net

记录异常时,它会在命令行中正确显示。但是,它不会写入数据库。其他参数记录正确。

在我的app.config中,例外配置如下:

<commandText value="INSERT INTO logs (time, log_level, logger, message, exception, thread) VALUES (?time, ?log_level, ?logger, ?message, ?exception, ?thread)" />

参数:

<parameter>
    <parameterName value="exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%exception" />
    </layout>
</parameter>

我正在测试:

try
{
    throw new DivideByZeroException();
}
catch (Exception e)
{
    log.Error("", e);
}  

我启用了Log4Net调试并获得了以下输出:

log4net: Hierarchy Threshold []
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
log4net:ERROR [CustomAdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
12:25:37,902 [Game.Program] {exceptions} () ERROR:  System.DivideByZeroException: Attempted to divide by zero.
   at Game.Program.Init() in --:line 98

log4net:ERROR [CustomAdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database
System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value)
   at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length)
   at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.Statement.SerialiSystem.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value)
   at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length)
   at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
   at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
   at MySql.Data.MySqlClient.Statement.BindParameters()
   at MySql.Data.MySqlClient.Statement.Execute()
   at MySql.Data.MySqlClient.PreparableStatement.Execute()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)
zeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
   at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
   at MySql.Data.MySqlClient.Statement.BindParameters()
   at MySql.Data.MySqlClient.Statement.Execute()
   at MySql.Data.MySqlClient.PreparableStatement.Execute()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
   at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)

1 个答案:

答案 0 :(得分:0)

我发现了错误。

显然,我添加了一些在调用我的测试异常函数时尚未初始化的全局变量。我将这些变量初始化为空字符串和零,然后就可以了。