使用自定义主题配置或扩展 log4net SmtpAppender

发布于 2024-09-27 01:04:12 字数 70 浏览 5 评论 0原文

如何为生成的电子邮件主题指定布局和转换模式?

BufferSize 需要小于或等于 1,这样就不会发生缓冲。

How can I specify a layout and conversionPattern for the resulting emails subject?

The BufferSize will need to be less than or equal to 1 so no buffering will occur.

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

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

发布评论

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

评论(4

伪装你 2024-10-04 01:04:12

CodeProject 文章 log4net NonBufferedSmtpAppenderWithSubjectLayout 看起来很有希望。


通过继承所需的基本附加程序(在我的例子中为 SmtpPickupDirAppender)并添加 ILayout 属性,可以更改 Append 方法中的主题。

public class SmtpSubjectLayoutPickupDirAppender : log4net.Appender.SmtpPickupDirAppender
{
    public SmtpSubjectLayoutPickupDirAppender()
        : base()
    {

    }

    public ILayout SubjectLayout
    {
        get;
        set;
    }

    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        if (this.BufferSize <= 1 && this.SubjectLayout != null)
        {
            StringWriter subjectWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
            this.SubjectLayout.Format(subjectWriter, loggingEvent);
            this.Subject = subjectWriter.ToString();
        }

        base.Append(loggingEvent);
    }
}

然后可以通过指定 subjectLayout 属性来配置它以覆盖默认主题。

<appender name="SmtpPickupDirAppender" type="namespace.for.SmtpSubjectLayoutPickupDirAppender">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="test logging message" />

    <subjectLayout type="log4net.Layout.PatternLayout, log4net">
        <conversionPattern value="Logging message - %message"/>
    </subjectLayout>

    <pickupDir value="C:\SmtpPickup" />
    <bufferSize value="1" />
    <lossy value="true" />
    <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
</appender>

The CodeProject article log4net NonBufferedSmtpAppenderWithSubjectLayout looks promising.


By inheriting from the required base appender (SmtpPickupDirAppender in my case) and adding a ILayout property it is possible to change the Subject in the Append method.

public class SmtpSubjectLayoutPickupDirAppender : log4net.Appender.SmtpPickupDirAppender
{
    public SmtpSubjectLayoutPickupDirAppender()
        : base()
    {

    }

    public ILayout SubjectLayout
    {
        get;
        set;
    }

    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        if (this.BufferSize <= 1 && this.SubjectLayout != null)
        {
            StringWriter subjectWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
            this.SubjectLayout.Format(subjectWriter, loggingEvent);
            this.Subject = subjectWriter.ToString();
        }

        base.Append(loggingEvent);
    }
}

This can then be configured by specifying a subjectLayout property to override the default subject.

<appender name="SmtpPickupDirAppender" type="namespace.for.SmtpSubjectLayoutPickupDirAppender">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="test logging message" />

    <subjectLayout type="log4net.Layout.PatternLayout, log4net">
        <conversionPattern value="Logging message - %message"/>
    </subjectLayout>

    <pickupDir value="C:\SmtpPickup" />
    <bufferSize value="1" />
    <lossy value="true" />
    <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
</appender>
诗化ㄋ丶相逢 2024-10-04 01:04:12

这是具有自定义主题的 SmtpAppender 的另一个示例。

Here is another example of SmtpAppender with custom subjects.

逐鹿 2024-10-04 01:04:12

因为之前的答案建议使用 NuGet 版本的 SmtpAppenderWithSubjectLayout 需要 log4net 版本 > 1.2.1,我使用了NuGet源代码,但将其修改为使用log4net 1.2.1

public class SmtpAppenderWithSubjectLayout : SmtpAppender
{
    public PatternLayout SubjectLayout { get; set; }

    protected override void SendBuffer(LoggingEvent[] events)
    {
        PrepareSubject(events);

        base.SendBuffer(events);
    }

    protected virtual void PrepareSubject(IEnumerable<LoggingEvent> events)
    {
        var subjects = new List<string>();

        foreach (LoggingEvent @event in events)
        {
            if (Evaluator.IsTriggeringEvent(@event))
            {
                var subjectWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
                SubjectLayout.Format(subjectWriter, @event);
                subjects.Add(subjectWriter.ToString());
            }
        }

        Subject = string.Join(", ", subjects.ToArray());
    }
}

Because the previous answeres that suggests using NuGet version of SmtpAppenderWithSubjectLayout needs log4net version > 1.2.1, I've used the NuGet source code but modified it to use the log4net 1.2.1

public class SmtpAppenderWithSubjectLayout : SmtpAppender
{
    public PatternLayout SubjectLayout { get; set; }

    protected override void SendBuffer(LoggingEvent[] events)
    {
        PrepareSubject(events);

        base.SendBuffer(events);
    }

    protected virtual void PrepareSubject(IEnumerable<LoggingEvent> events)
    {
        var subjects = new List<string>();

        foreach (LoggingEvent @event in events)
        {
            if (Evaluator.IsTriggeringEvent(@event))
            {
                var subjectWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
                SubjectLayout.Format(subjectWriter, @event);
                subjects.Add(subjectWriter.ToString());
            }
        }

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