From 0c4e916693456d541b6ed327123d3a832419bfa5 Mon Sep 17 00:00:00 2001 From: Tsar Nikolay Date: Tue, 23 Nov 2021 12:58:31 +0500 Subject: [PATCH] Fix AdoNetAppender using npgsql once again. --- src/log4net/Appender/AdoNetAppender.cs | 28 +++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/log4net/Appender/AdoNetAppender.cs b/src/log4net/Appender/AdoNetAppender.cs index 2f6edfbb..b5d05842 100644 --- a/src/log4net/Appender/AdoNetAppender.cs +++ b/src/log4net/Appender/AdoNetAppender.cs @@ -557,23 +557,26 @@ protected virtual void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) try { // prepare the command, which is significantly faster - dbCmd.Prepare(); + Prepare(dbCmd); } catch (Exception) { + if (dbTran != null) + { + // rethrow exception in transaction mode, cuz now transaction is in failed state + throw; + } + // ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql } // run for all events foreach (LoggingEvent e in events) { - // clear parameters that have been set - dbCmd.Parameters.Clear(); - + // No need to clear dbCmd.Parameters, just use existing. // Set the parameter values foreach (AdoNetAppenderParameter param in m_parameters) { - param.Prepare(dbCmd); param.FormatValue(dbCmd, e); } @@ -606,6 +609,21 @@ protected virtual void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) } } + /// + /// Prepare entire database command object to be executed. + /// + /// The command to prepare. + protected virtual void Prepare(IDbCommand dbCmd) + { + // npgsql require parameters to prepare command + foreach (AdoNetAppenderParameter parameter in m_parameters) + { + parameter.Prepare(dbCmd); + } + + dbCmd.Prepare(); + } + /// /// Formats the log message into database statement text. ///