gpt4 book ai didi

configuration - 配置 log4net 根据级别将错误发送到不同的appender

转载 作者:行者123 更新时间:2023-12-03 07:06:01 25 4
gpt4 key购买 nike

我想将信息级别及以上发送到 XML 附加程序,并将错误/致命级别发送到事件日志附加程序。

我认为我需要修改配置的根元素,但我在语法上遇到了困难。对于给定级别或级别范围,将日志定向到正确的附加程序的配置语法是什么?

这是到目前为止的配置:

<log4net>
<appender name="SomeXmlAppender" type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="SomeEventLogAppender" type="log4net.Appender.EventLogAppender">
...
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="SomeXmlAppender" />
<appender-ref ref="SomeEventLogAppender" />
</root>
</log4net>

编辑:谢谢@agileguy。那篇文章确实包含了我需要的语法。现在的工作解决方案如下所示:

<log4net>
<appender name="SomeXmlAppender" type="log4net.Appender.RollingFileAppender">
...
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="INFO"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="SomeEventLogAppender" type="log4net.Appender.EventLogAppender">
...
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="SomeXmlAppender" />
<appender-ref ref="SomeEventLogAppender" />
</root>
</log4net>

最佳答案

这可以使用 threshold 来完成或filter附加程序中的元素。

请注意,阈值可以直接位于附加程序下方(充当包含过滤器),也可以位于 evaluator 下方。例如

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

在适用的情况下,它充当跳过缓冲(立即输出)的包容性过滤器。


<小时/>

完整说明(source):

<threshold value="ERROR" />

The Threshold is implemented in the AppenderSkeleton and therefore supported by almost all appenders. It is just a simple test that is used to ignore logging events that have a level below the threshold. The threshold is checked early and as a simple test is very performant.

There is another way to specify the same behaviour as the threshold using filters. Filters are very much more flexible and as they are pluggable you can also develop your own custom logic and insert it into the filter chain.

<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="OFF" />
</filter>

与阈值检查过滤器一样,过滤器也在 AppenderSkelton 基类中实现,并且几乎所有附加程序都支持。上述过滤器与<threshold value="ERROR" />效果相同。它是一个 LevelRangeFilter,允许通过级别在 ERROR 范围内的任何事件。至OFF (包括的)。请注意OFF是最高级别的名称,反之ALL是最低级别的名称。

Filters have a great deal of flexibility because multiple filters can be chained together to provide fine grained control over the events that are output. Because of this they also have a higher cost in terms of performance, each filter in the chain is an object and is asked to decide on the correct course of action. In the simple case of the threshold filtering the Threshold property should be used in preference to a filter.

The Evaluator is implemented by the BufferingAppenderSkeleton and is therefore only supported by appenders that extend this base class and provide support for buffering. The SmtpAppender is one such appender.

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. The evaluator does not function like the threshold or a filter in that it does not discard events.

关于configuration - 配置 log4net 根据级别将错误发送到不同的appender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1388588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com