简体   繁体   English

使用log4net以批量方式登录到SQL Server

[英]Logging to SQL Server in bulks using log4net

I'm using log4net to log messages to a SQL Server database using the AdoNetAppender . 我正在使用log4net使用AdoNetAppender将消息记录到SQL Server数据库。 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: 我已经尝试使用appender的batchSize属性,但据我所知,结果是这样的:

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. 因此,如果我的批量大小是例如500,则将有500个连续插入。

My goal is for the bulks to be inserted in 1 insert statement. 我的目标是将插件插入1个插入语句中。 Like .NET's SqlBulkCopy . 就像.NET的SqlBulkCopy

Is it possible to achieve this result with log4net? 用log4net可以实现这个结果吗? Or do I have to implement my own custom appender to do so? 或者我是否必须实现自己的自定义appender才能这样做?

You have to write your own custom appender because log4net does not do this out of the box. 您必须编写自己的自定义appender,因为log4net不会开箱即用。 You can just inherrit from the AdoNetAppender and override the virtual protected void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events) 您可以从AdoNetAppender继承并覆盖virtual protected void SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)

AdoNetAppender.cs AdoNetAppender.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM