简体   繁体   English

Log4net 缓冲区在有损设置中已满时不会刷新

[英]Log4net buffer not flushing when full in lossy setup

I'm using Log4net ElasticSearchAppender in my C# webAPI with a BufferSize of 10 and Lossy set to true to preserve performance, as seen here : https://github.com/bruno-garcia/log4net.ElasticSearch/wiki/02-Appender-Settings我在 C# webAPI 中使用 Log4net ElasticSearchAppender, BufferSize10Lossy设置为true以保持性能,如下所示: https : //github.com/bruno-garcia/log4net.ElasticSearch/wiki/02-Appender-设置

<lossy value="false"/> Log4net.ElasticSearch uses a buffer to collect events and then flush them to the Elasticsearch server on a background thread. <lossy value="false"/> Log4net.ElasticSearch 使用缓冲区来收集事件,然后将它们刷新到后台线程上的 Elasticsearch 服务器。 Setting this value to true will cause log4net.Elasticsearch to begin discarding events if the buffer is full and has not been flushed.如果缓冲区已满且尚未刷新,则将此值设置为 true 将导致 log4net.Elasticsearch 开始丢弃事件。 This could happen if the Elasticsearch server becomes unresponsive or goes offline.如果 Elasticsearch 服务器无响应或脱机,则可能会发生这种情况。

I also set the evaluator to ERROR , that will force the flushing of the buffer anyway if an ERROR occurs.我还将评估器设置为ERROR ,如果发生 ERROR ,无论如何都会强制刷新缓冲区。

Here's the associated config file :这是相关的配置文件:

<?xml version="1.0"?>
<log4net>
    <appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
    <threshold value="ALL" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
    </layout>
    <connectionString value="Server=my-elasticsearch-server;Index=foobar;Port=80;rolling=true;mode=tcp"/>
    <lossy value="true" />
    <bufferSize value="10" />
    <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="ERROR" />
    </evaluator>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="ElasticSearchAppender" />
  </root>
</log4net>

Here's the behaviour I get : the flushing triggered by an ERROR (evaluator) is working fine, but INFO or DEBUG messages alone are never flushed to Elastic , even if there are 10, 20, or 100 of them.这是我得到的行为:由 ERROR(评估器)触发的刷新工作正常,但INFODEBUG消息本身永远不会单独刷新到 Elastic ,即使其中有 10、20 或 100 个。

The buffer does never flush when full in this configuration, it just keeps discarding DEBUG or INFO logs until an ERROR comes out, even though Elastic is online and perfectly responsive.在此配置中,缓冲区在满时永远不会刷新,它只会不断丢弃DEBUGINFO日志,直到出现ERROR ,即使 Elastic 在线且响应良好。

Note: I tried setting lossy to false, and the buffer flushes when full.注意:我尝试将lossy设置为 false,并且缓冲区在满时刷新。 But I'm affraid this would damage my application responsiveness too much.但我担心这会严重损害我的应用程序响应能力。

Am I gettings something wrong?我有什么问题吗? Is there a better way to log while minimizing performance impact?有没有更好的日志记录方式,同时最大限度地减少性能影响?

After testing the behaviour, here's what I found :测试行为后,这是我发现的:

The buffer becoming full does never trigger a flushing when lossy is true.lossy为真时,缓冲区变满永远不会触发刷新。

Bruno garcia 's article was quite misleading about the Lossy property, especially this sentence : Bruno garcia的文章对Lossy属性相当误导,尤其是这句话:

Setting this value to true will cause (...) to begin discarding events if the buffer is full (...).如果缓冲区已满 (...),则将此值设置为 true 将导致 (...) 开始丢弃事件。 This could happen if the Elasticsearch server becomes unresponsive or goes offline.如果 Elasticsearch 服务器无响应或脱机,则可能会发生这种情况

In fact it has nothing to do with the appender/Elastic being unresponsive : in a lossy configuration, only evaluators will trigger the flushing of the buffer :事实上,它与 appender/Elastic 无响应无关:在有损配置中,只有评估器会触发缓冲区的刷新:

  • Level evaluator, will flush if an event of a certain lever occurs (ex: FATAL or ERROR), giving the context of the crash (=last logs occuring before the crash).级别评估器,如果发生某个级别的事件(例如:致命或错误),将刷新,给出崩溃的上下文(=崩溃前发生的最后一个日志)。

     <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="ERROR"/> </evaluator>
  • Time evaluator will flush if a certain time interval has elapsed如果某个时间间隔已过,时间评估器将刷新

    <evaluator type="log4net.Core.TimeEvaluator"> <interval value="300"/> </evaluator>

For my purpose I finally decided to configure a TimeEvaluator with a 5 minutes interval.出于我的目的,我最终决定配置一个间隔为 5 分钟的 TimeEvaluator。 This way, as long as there is no more than 200 logs (my buffer size) per 5 minutes, no log will be discarded and the impact on performance is kept low.这样,只要每 5 分钟不超过 200 条日志(我的缓冲区大小),就不会丢弃任何日志,并且对性能的影响保持在较低水平。

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

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