Log4Net +完成后发送电子邮件?

发布于 2024-09-29 18:23:23 字数 1234 浏览 0 评论 0原文

我刚刚开始使用 Log4Net...我现在想发送一封电子邮件,其中附有完整的日志或直接在邮件中发送。使用 SmtpAppender 的问题是它需要一个未知的 bufferSize,因为它应该发送邮件,无论邮件充满错误还是只是信息。

更新:我的配置文件

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">

    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Backup Application - Log" />
    <smtpHost value="mailserver" />
    <authentication value="1" />
    <username value="userName" />
    <password value="mypw" />

    <port value ="25"/>
    <lossy value="true" />
    <bufferSize value="500" />

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

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%timestamp [%thread] %-5level %logger – %message%newline" />
    </layout>

</appender>

I have just started playing around with Log4Net... I now want to send an email with the full log either attached or directly in the mail. The problem with using SmtpAppender is that it requires a bufferSize which will be unknown because it should send the mail whether it's full of errors or just info.

Update: My config file

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">

    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Backup Application - Log" />
    <smtpHost value="mailserver" />
    <authentication value="1" />
    <username value="userName" />
    <password value="mypw" />

    <port value ="25"/>
    <lossy value="true" />
    <bufferSize value="500" />

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

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%timestamp [%thread] %-5level %logger – %message%newline" />
    </layout>

</appender>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

想挽留 2024-10-06 18:23:23

BufferSize 等于必须缓冲的日志消息数(即,如果设置为 512,则一旦收集到 512 条消息,就会发送邮件)。

我相信将其设置为 int.MaxValue (即 2.147.483.647)是一个合理的选择。对于一个系统来说,20 亿条消息太多了,即使是长时间运行的消息也是如此。

如果您给我 10 分钟的时间,我将向您确认(从源代码),如果您干净停止您的应用程序,迄今为止收集的所有日志都将发送

[更新]:已确认!!析构函数按预期刷新队列

[添加]我将删除有损和求值器。您的问题很清楚:评估器优先于缓冲区:):)

评估器用于在满足特定条件时刷新队列。您的条件等于true。当触发此条件时,将发送电子邮件,因此这就是在每次日志调用时发送邮件的原因。

这与仅发送信息和错误消息略有不同,这是通过日志过滤实现的。

删除这两个属性,您的代码就可以工作了。设置 int.MaxValue 将允许您存储最大可能数量的消息。应用程序在一次运行中收集超过 20 亿个错误/信息是不太可能的(你最好像今晚有些人那样赢得 Superenalotto 的 1.78 亿欧元大奖,或者被一颗彗星击中)。

BufferSize equals to the number of log messages that have to be buffered (ie if you set to 512 the mail will be sent once 512 messages have been collected).

I believe that setting it to int.MaxValue (which is 2.147.483.647) is a reasonable choice. 2 billions of messages are too much for a system, even long-running.

If you give me 10 minutes I'll confirm you (from source code) that if you clean stop your application, all logs collected so far will be sent

[Update]: confirmed!! Destructor flushes the queue as expected

[Add] I would remove both lossy and evaluator. Your problem is clear: Evaluator has precedence over buffer :) :)

The evaluator is used to flush the queue when a certain condition is met. Your condition equals to true. When this condition is triggered, the email is sent, so this is why the mail is sent at every single log call.

This is slightly different from sending ONLY info and error messages, which is achieved by log filtering.

Remove the two attributes and your code will work. Setting int.MaxValue will allow you to store the maximum possible number of messages. It's unlikely (you'd better win the Superenalotto's 178mln€ jackpot like some guys did tonight, or being hit by a comet in your head) that an application collects more than 2 billion errors/info in a run.

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