Logging to SQL Server in bulks using log4net

adonetappender c# log4net sqlbulkcopy sql-server

Question

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?

Accepted Answer

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




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why