Nlog: Is there a way to create a buffer that is triggered?

Created on 14 Oct 2016  路  8Comments  路  Source: NLog/NLog

Is it possible to setup nLog to buffer the last X messages, but then only send them to the actual targets based on some condition or trigger. Say if an ERROR or WARN event occurs? I don't want to continuously log at a DEBUG level, but it would be useful if I had the previous DEBUG messages after an error or warning occurs.

question

All 8 comments

You could do this, but you need to program it in c# its pretty easy.

This is possible through the configuration in Log4Net. Could this possibly be a feature request for nLog?

In Log4Net the BufferingAppenderSkeleton class can be assigned an elevator which triggers an immediate flush of the buffer. In addition, it can also be set to "lossy", meaning when the buffer fills up it will discard the older messages rather then flushing.

Do you have a proposal how the config should look like ?

How about adding lossy and flushCondition properties to BufferingWrapper.

Setting lossy to true would cause the Buffer to just discard old messages rather then flushing when the buffer fills up. flushCondition would be a condition that when satisfied causes the buffer to flush immediately rather then waiting for the next flushTimeout.

<targets>
  <target xsi:type="BufferingWrapper"
          name="String"
          slidingTimeout="Boolean"
          bufferSize="Integer"
          flushTimeout="Integer"
          lossy="Boolean"> 
          flushCondition="Condition">
          <target xsi:type="wrappedTargetType" ...target properties... />
  </target>
</targets>

Here's an example configuration

<targets>
  <target xsi:type="BufferingWrapper"
          name="ErrorFlushingBuffer"
          slidingTimeout="False"
          bufferSize="100"
          flushTimeout="-1"  <!-- Disabled timed flushes so flush only occurs when flushCondition is met -->
          lossy="true"> <!-- Enable lossy, old messages are discarded when buffer fills up-->
          flushCondition="level>LogLevel.Info">  <!-- Flush the buffer when a Warn, Error, or Fatal message is logged-->

          <target xsi:type="wrappedTargetType" ...target properties... />
  </target>
</targets>

I like the flush condition.

Instead of Lossy, could we add the property "overflow" (options: flush, discard) ? - that would be more consistent with other targets.

Yes I like naming it overflow better as well.

OK then :)

I have added new feature request: https://github.com/NLog/NLog/issues/1712

This won't have high prio know. We have still a lot to do with .NETSTANDARD and structural logging. PRs are acccepted (when proper unit tested).

Closing this question as there is a feature request issue :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vasumsit picture vasumsit  路  3Comments

smeegoan picture smeegoan  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

Rapiiidooo picture Rapiiidooo  路  3Comments

BobSeu picture BobSeu  路  3Comments