0
votes

I have two loggers for same database with different level. I would like to have different bufferSize for each logger. One way is to have two appenders to same database with only difference in bufferSize element, but it's copy-paste. Is it possible to extend already defined appender and change it's bufferSize property? For example:

 <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="20" />
      ...other elements
</appender>
<appender name="AdoNetAppenderChild" extends="AdoNetAppender">
  <bufferSize value="1" />
</appender>

    <logger name="Fatal" additivity="false">
  <level value="FATAL"/>
  <appender-ref ref="AdoNetAppenderChild" />
</logger>
    <logger name="Common" additivity="false">
  <level value="INFO"/>
  <appender-ref ref="AdoNetAppender" />
</logger>

What I want to avoid is having two appenders with same elements and properties and only different value is bufferSize

1
"it's copy-paste" Config very often is, as well as being annoyingly verbose and repetitive.. In this situation doubling up in the config is probably your best bet though - you really don't want to change the buffer size at runtime, and the only other alternative is to write code to duplicate the appender (or create a new one etc), which then needs to be documented, tested etc.stuartd
Thanks. One more question, is it save to have single static logger for every MVC controller and in that logger add some extra information from session. Specificly, if I put log4net.LogicalThreadContext.["sessionVariable"] from session, can I be sure it won't get overwritten or should on every request create new logger instance?Error
I wouldn't be 100% confident about that with a static logger. log4net caches loggers so creating a new on on each request would be safer.stuartd

1 Answers

0
votes

You can make one appender and use a evaluator to log when you have an error message:

<evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="ERROR"/>
</evaluator>

The Evaluator is a pluggable object that is used by the BufferingAppenderSkeleton to determine if a logging event should not be buffered, but instead written/sent immediately. If the Evaluator decides that the event is important then the whole contents of the current buffer will be sent along with the event. Typically an SmtpAppender will be setup to buffer events before sending as the cost of sending an email may be relatively high. If an important event arrives, say an ERROR, we would like this to be delivered immediately rather than waiting for the buffer to become full. This is where the Evaluator comes in as it allows us to say: "when an important event arrives don't worry about buffering, just send over everything you have right now".