2016-03-10 144 views
0

當記錄異常時,它在命令行中正確顯示。但是,它不寫入數據庫。其他參數記錄正確。在向MySQL數據庫寫入異常時發生Log4Net異常

在我的app.config,異常配置如下:

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

參數:

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

我啓用log4net的調試,並得到了:

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

我與測試以下輸出:

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) 
+0

看起來像一個值,你試圖插入的問題。我認爲你有一列NUMBER類型。你能發佈你試圖插入列的值嗎?或者更好的是,所有綁定變量的值。 –

+0

直接詢問什麼是?time,?log_level,?logger,?message,?exception,?線程變量的值? –

+0

這些都是Log4Net的內部值。除異常外,它們正確記錄到數據庫。 – Z0q

回答

0

我發現了錯誤。

顯然,我添加了一些全局變量,這些變量在調用我的測試異常函數時尚未初始化。我初始化這些變量爲空字符串和零,它的工作。

相關問題