protected virtual void SendBuffer()

in src/log4net/Appender/AdoNetAppender.cs [466:527]


  protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
  {
    events.EnsureNotNull();
    if (!string.IsNullOrWhiteSpace(CommandText))
    {
      using IDbCommand dbCmd = Connection.EnsureNotNull().CreateCommand();
      dbCmd.CommandText = CommandText;
      dbCmd.CommandType = CommandType;
      if (dbTran is not null)
      {
        dbCmd.Transaction = dbTran;
      }
      try
      {
        // prepare the command, which is significantly faster
        Prepare(dbCmd);
      }
      catch (Exception)
      {
        if (dbTran is not 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)
      {
        // No need to clear dbCmd.Parameters, just use existing.
        // Set the parameter values
        foreach (AdoNetAppenderParameter param in m_parameters)
        {
          param.FormatValue(dbCmd, e);
        }

        // Execute the query
        dbCmd.ExecuteNonQuery();
      }
    }
    else
    {
      // create a new command
      using IDbCommand dbCmd = Connection!.CreateCommand();
      if (dbTran is not null)
      {
        dbCmd.Transaction = dbTran;
      }
      // run for all events
      foreach (LoggingEvent e in events)
      {
        // Get the command text from the Layout
        string logStatement = GetLogStatement(e);

        LogLog.Debug(_declaringType, $"LogStatement [{logStatement}]");

        dbCmd.CommandText = logStatement;
        dbCmd.ExecuteNonQuery();
      }
    }
  }