为什么有时虽然已经配置了缓冲区大小,但Log4net事件有时仍使用随机事件计数进行记录? [英] Why sometimes Log4net events are logging with random event count, though buffer size is already configured?
问题描述
我已经在自定义记录器类中实现了BufferingAppenderSkeleton,并重写了SendBuffer方法以在数据库中记录批处理日志.
I've implemented BufferingAppenderSkeleton in my custom logger class and override SendBuffer method for logging batch logs in the database.
这是一个实现:
protected override void SendBuffer(LoggingEvent[] events)
{
List<Log> logs = new List<Log>();
if (events != null && events.Length > 0)
{
foreach (var log in events)
{
var logWithLogSessionId = GetLogWithLogSession(log);
logs.Add(logWithLogSessionId);
}
}
Analytics.SaveLogs(logs);
}
这里是log4net的web.config配置:
And here web.config configuration for log4net:
<log4net>
<appender name="LogAppender" type="MyApp.LogAppender">
<bufferSize value="20"/>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="LogAppender"/>
</root>
根据文档,此方法将仅触发当事件计数达到配置的缓冲区大小时,但是有时这会使用随机事件计数进行调用.
As per documentation, this method will trigger only when event count reaches equal to configured buffer size but sometimes this is calling with random event counts.
例如 有时在缓冲区中只有1个事件时触发,有时在事件计数达到缓冲区大小+ 1等时触发?
eg. Sometimes triggers when there is only 1 event in the buffer, sometimes when event count reached buffer size + 1 etc?
推荐答案
BufferSize
属性表示可以存储在内存中的事件数,而不是要在SendBuffer
方法中发送的事件数.如果将BufferSize
设置为20,则第21个事件将触发该方法,并与其他事件一起发送.检查
BufferSize
property represents number of events that can be stored in memory, not number of event to be send in SendBuffer
method. If you set BufferSize
to 20, than 21st event will trigger that method and will be sent with other events as well. Check SendFromBuffer method in BufferingAppenderSkeleton
class.
因此,基本上,如果要批量发送20个事件,请将BufferSize
设置为19.
So basically, if you want to send 20 events in batch, set your BufferSize
to 19.
这似乎是错误的,但是您需要查看BufferingAppenderSkeleton
中的Lossy
和Evaluator
属性如何工作.如果将Lossy
设置为true
,则您将仅在内存中保留20条最新/最重要的消息,而忽略最旧的事件,直到Evaluator
触发SendBuffer
.检查追加方法的实现完全理解这一点.
This might seems wrong, but you will need to see how Lossy
and Evaluator
properties in BufferingAppenderSkeleton
works. If you set Lossy
to true
, then you will only keep 20 latest/most important messages in memory, and disregards oldest events until Evaluator
triggers SendBuffer
. Check implementation of Append method to fully understand this.
这篇关于为什么有时虽然已经配置了缓冲区大小,但Log4net事件有时仍使用随机事件计数进行记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!