如何使用 StringMatchFilter 根据 log4net 中的消息内容将日志记录事件发送到不同的日志?
我正在使用 log4net 来记录和发送电子邮件。我需要根据不同的流程发送不同配置的不同电子邮件。例如,当我的一个方法完成处理时,它会向 X 发送一封邮件,而当我的服务成功完成时,它会向 Y 发送另一封邮件。
我认为我需要使用不同的附加程序和过滤器,但我无法正确配置它们。
我尝试了以下操作并尝试应用 StringMatchFilter ,以便包含“强制”的消息不会传递到 Y,包含“成功”的消息不会传递到 X,但没有运气。我也不确定这种过滤是否是进行这种配置的正确方法。
<log4net>
<appender name="ManFieldMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="successfully" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="[email protected]" />
<from value="[email protected]" />
<subject value="Products not made online" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<appender name="ServiceFinishedMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="mandatory" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="[email protected]" />
<from value="[email protected]" />
<subject value="Product Publish Service has successfully finished operating" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<root>
<appender-ref ref="ManFieldMailAppender" />
<appender-ref ref="ServiceFinishedMailAppender"/>
</root>
</log4net>
您认为问题出在哪里?
谢谢。
I'm using log4net for logging and sending e-mails. I need to send different e-mails with different configurations according to different processes. For example, when one of my methods finishes processing, it sends a mail to X, and when my service finishes successfully, it sends another mail to Y.
I think I need to use different appenders with filters, but I can't get to configure them properly.
I tried the following and tried to apply a StringMatchFilter so that messages containing "mandatory" will not be delivered to Y and messages containing "successfully" will not be delivered to X, but with no luck. I am also not sure whether this kind of filtering is the right way to do this kind of configuration.
<log4net>
<appender name="ManFieldMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="successfully" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="[email protected]" />
<from value="[email protected]" />
<subject value="Products not made online" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<appender name="ServiceFinishedMailAppender" type="log4net.Appender.SmtpAppender,log4net">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="mandatory" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<to value="[email protected]" />
<from value="[email protected]" />
<subject value="Product Publish Service has successfully finished operating" />
<smtpHost value="Host" />
<authentication value="1"/>
<username value="uid" />
<password value="pwd" />
<port value="25"/>
<bufferSize value="1" />
<lossy value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%newline %date %message%newline%newline%newline" />
</layout>
</appender>
<root>
<appender-ref ref="ManFieldMailAppender" />
<appender-ref ref="ServiceFinishedMailAppender"/>
</root>
</log4net>
What do you think the problem is?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
评估器
告诉缓冲附加程序何时刷新。从您的配置来看,我认为您没有缓冲。 (因此您也可以摆脱bufferSize
和lossy
参数)。我看到您的附加器过滤器链具有所需的DenyAllFilters
。但是,您的根记录器配置未显示任何级别,请尝试添加一般情况下,我不会根据消息文本决定记录到特定的附加程序。看起来您对其中一项操作拥有高级服务级别。如果您对需要发送的其他消息有相同的事情,并且您按类型名称命名记录器,则可以为每个记录器指定一个附加程序。
First,
evaluator
tells buffering appenders when to flush. From the look of your configuration I don't think you're buffering. (so you can get rid of thebufferSize
andlossy
parameters, too). I see your appender filter chains have the requiredDenyAllFilters
. But, your root logger configuration is showing no levels, try addingIn general, I wouldn't make the decision to log to a particular appender based on the message text. It looks like you have a high-level service class for one of the operations. If you have the same thing for the other messages that need to be sent, and you're naming your loggers by typename, you can specify an appender per logger.