简体   繁体   中英

Customizing the body of the email using Log4net Smtp appender

How do I customize the body of the email using Log4net Smtp appender? I wanted to add custom message to the body.

Given you are using an appender similar this

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@domain.com" />
    <from value="from@domain.com" />
    <subject value="test logging message" />
    <smtpHost value="SMTPServer.domain.com" />
    <bufferSize value="512" />
    <lossy value="false" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
    </layout>
</appender>

You should be able to format the message using StringBuilder before logging:

var sb = new StringBuilder();
sb.Append("Header");
sb.Append(Environment.NewLine);
sb.Append("Message");
...
var msg = sb.ToString();

ILog log = //resolve ILog
log.Debug(msg);

More config samples here search for SmtpAppender

You can create your own appender and inherit it from SmtpAppender. There you can override such methods as SendEmail and so on.

class MySmtpAppender : SmtpAppender
{
    protected override void SendEmail(string messageBody)
    {
        string newmessageBody = messageBody + "...";
        base.SendEmail(newmessageBody);
    }
}

<appender name="MySmtpAppender" type="YourLib.MySmtpAppender">

You can also add some extra properties to this class and you will be able to use them in your config file.

When you use an SMTP Appender the message body contains the log event, Which is formatted by the layout you specify when you configure the appender.

The most used one is PatternLayout so whatever you put in this layout's conventionPattern property will go into the email message body.

so you should do something like this:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="to@domain.com" />
    <from value="from@domain.com" />
    <subject value="test logging message" />
    <smtpHost value="SMTPServer.domain.com" />
    <bufferSize value="512" />
    <lossy value="false" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="Hello, we got an error in the app. here are the details:  %newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>
</appender>

You can customize at layout level for example by adding an header and/or a footer:

<layout type="log4net.Layout.PatternLayout">
    <header value="[Header]&#13;&#10;" />
    <footer value="[Footer]&#13;&#10;" />
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>

The body is controlled using a conversion pattern (like any other appender).

<appender name="EmailAppender" type="log4net.Appender.SmtpAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%n%n%d{yyyy-MM-dd HH:mm:ss} %5p %10u %m" />
          </layout>
</appender>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM