如何使用 StringMatchFilter 根据 log4net 中的消息内容将日志记录事件发送到不同的日志?

发布于 2024-10-13 22:51:09 字数 3598 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-10-20 22:51:09

首先,评估器 告诉缓冲附加程序何时刷新。从您的配置来看,我认为您没有缓冲。 (因此您也可以摆脱 bufferSizelossy 参数)。我看到您的附加器过滤器链具有所需的 DenyAllFilters。但是,您的根记录器配置未显示任何级别,请尝试添加

<root>
    <level value="ALL" />
    <!-- appender-refs as normal -->
</root>

一般情况下,我不会根据消息文本决定记录到特定的附加程序。看起来您对其中一项操作拥有高级服务级别。如果您对需要发送的其他消息有相同的事情,并且您按类型名称命名记录器,则可以为每个记录器指定一个附加程序。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文