1

I'm using log4net to log messages to a SQL Server database using the AdoNetAppender. I need to insert the logs in bulks. I've tried using the batchSize property of the appender, but as far as I can tell the result is something like:

conn.Open();

for (int i = 0; i < count; i++)
{
    comm.ExecuteNonReader();
}

conn.Close();

And this eventually translates to a bunch of Inserts. So if my batch size is for instance 500, then there will be 500 consecutive inserts.

My goal is for the bulks to be inserted in 1 insert statement. Like .NET's SqlBulkCopy.

Is it possible to achieve this result with log4net? Or do I have to implement my own custom appender to do so?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Remoba
  • 147
  • 1
  • 8

1 Answers1

3

You have to write your own custom appender because log4net does not do this out of the box. You can just inherrit from the AdoNetAppender and override the virtual protected void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)

AdoNetAppender.cs

Peter
  • 27,590
  • 8
  • 64
  • 84
  • Thanks for the answer. I ended up using the following implementatio https://github.com/cneuwirt/rhino-commons/blob/master/Rhino.Commons/Logging/AsyncBulkInsertAppender.cs – Remoba Dec 17 '15 at 17:39
  • Hi Peter, do you have an example of doing a bulk insert using IDbTransaction? – Dave Lawrence Aug 09 '17 at 11:23
  • @dave sorry I don't have such example. Adding a transaction around the insert doesn't make sense in a logging scenario. A rollback can remove logentries. – Peter Aug 09 '17 at 16:04