log4net 过滤异常消息?
如何根据记录的异常消息过滤日志记录?
代码如下所示:
try {
someService.DoSomeWorkflow();
} catch(Exception e) {
log.Error("Hey I have an error", e);
}
配置如下:
<appender name="EventLogger" type="log4net.Appender.EventLogAppender">
<applicationName value="foo" />
<layout type="log4net.Layout.PatternLayout" value="PID:%P{pid}: %message" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="TextInsideTheException" />
</filter>
</appender>
我发现我只能过滤记录的消息(“嘿,我有一个错误”),但它似乎忽略了异常的消息。由于这是在我们的生产环境中,我无法进行任何代码更改,因此我无法更改记录的消息。是否有一些配置指定还检查异常消息?
How can I filter logging based on a logged exception's message?
Code looks like this:
try {
someService.DoSomeWorkflow();
} catch(Exception e) {
log.Error("Hey I have an error", e);
}
Config looks like this:
<appender name="EventLogger" type="log4net.Appender.EventLogAppender">
<applicationName value="foo" />
<layout type="log4net.Layout.PatternLayout" value="PID:%P{pid}: %message" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="TextInsideTheException" />
</filter>
</appender>
I'm finding that I can filter only on the logged message ("Hey I have an error") but it seemingly ignores the exception's message. Since this is in our production environment I can't make any code changes so I can't change the logged message. Is there some configuration that would specify to also check the exception's message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过子类化 FilterSkeleton,您可以实现一个过滤器评估异常文本。或者就此而言的异常类型。
By subclassing FilterSkeleton, you can implement a filter that evaluates the exception text. Or exception type for that matter.
以下是基于 Peter 接受的答案
配置文件的基本实现
Here are basic implementations based on Peter's accepted answer
Configuration file
试试这个:
编辑:抱歉,没有看到您无法更改该行...
Try this:
Edit: Sorry, didn't see that you cannot change that line...