How do I use a StringMatchFilter to send logging events to different logs by their message content in 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="x@xxx.com" />
<from value="aaa@aaa.com" />
<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="y@yyy.com" />
<from value="bbb@bbb.com" />
<subject value="Pro开发者_如何学JAVAduct 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.
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 the bufferSize
and lossy
parameters, too). I see your appender filter chains have the required DenyAllFilters
. But, your root logger configuration is showing no levels, try adding
<root>
<level value="ALL" />
<!-- appender-refs as normal -->
</root>
In 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.
<logger name="MyCompany.Product.Whatever">
<level value="WARN" />
<appender name="ManFieldMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>
<logger name="MyCompany.Services.Publish">
<level value="WARN" />
<appender name="ServiceFinishedMailAppender" type="...">
<!-- the rest of the configuration, without filtering goes here -->
</appender>
</logger>
精彩评论