log4net的HTML格式的SmtpAppender(Log4net html formatted

2019-08-19 16:06发布

我想给我在HTML格式的电子邮件。

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

  <to value="zzz@zzz.com"/>
  <from value="do_not_reply@zz.com"/>
  <subject value="zzzAdmin Logged Event" />
  <smtpHost value="email.zzz.com"/>
  <bufferSize value="1"/>
  <lossy value="false"/>
  <authentication value="Basic" />

我该错误记录为HTML格式的文本,但电子邮件客户端渲染HTML为纯文本。

我怎么能告诉SMTPAppender到HTML类型添加到电子邮件,以便客户端将呈现消息,它是HTML。

Answer 1:

内置的SmtpAppender不支持此所以你需要推出自己的appender。



Answer 2:

Since log4net isn't supporting HTML format in the message body so far, I wrote my own appender:

using System;
using System.Net.Mail;
using log4net.Appender;

namespace log4net.Appender
{
    public class ExtendedSmtpAppender : SmtpAppender
    {
        public bool IsBodyHtml { get; set; }

        protected override void SendEmail(string messageBody)
        {
            // .NET 2.0 has a new API for SMTP email System.Net.Mail
            // This API supports credentials and multiple hosts correctly.
            // The old API is deprecated.

            // Create and configure the smtp client
            SmtpClient smtpClient = new SmtpClient();
            if (!String.IsNullOrEmpty(SmtpHost))
            {
                smtpClient.Host = SmtpHost;
            }
            smtpClient.Port = Port;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = EnableSsl;

            if (Authentication == SmtpAuthentication.Basic)
            {
                // Perform basic authentication
                smtpClient.Credentials = new System.Net.NetworkCredential(Username, Password);
            }
            else if (Authentication == SmtpAuthentication.Ntlm)
            {
                // Perform integrated authentication (NTLM)
                smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            }

            using (MailMessage mailMessage = new MailMessage())
            {
                mailMessage.IsBodyHtml = IsBodyHtml;
                mailMessage.Body = messageBody;
                //mailMessage.BodyEncoding = BodyEncoding;
                mailMessage.From = new MailAddress(From);
                mailMessage.To.Add(To);
                if (!String.IsNullOrEmpty(Cc))
                {
                    mailMessage.CC.Add(Cc);
                }
                if (!String.IsNullOrEmpty(Bcc))
                {
                    mailMessage.Bcc.Add(Bcc);
                }
                if (!String.IsNullOrEmpty(ReplyTo))
                {
                    // .NET 4.0 warning CS0618: 'System.Net.Mail.MailMessage.ReplyTo' is obsolete:
                    // 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'
#if !FRAMEWORK_4_0_OR_ABOVE
                    mailMessage.ReplyTo = new MailAddress(ReplyTo);
#else
                    mailMessage.ReplyToList.Add(new MailAddress(m_replyTo));
#endif
                }
                mailMessage.Subject = Subject;
                //mailMessage.SubjectEncoding = m_subjectEncoding;
                mailMessage.Priority = Priority;

                // TODO: Consider using SendAsync to send the message without blocking. This would be a change in
                // behaviour compared to .NET 1.x. We would need a SendCompletedCallback to log errors.
                smtpClient.Send(mailMessage);
            }
        }
    }
}

To use this appender in your log4net config, just replace namespace and class name of your standard SmtpAppender definition and add the isBodyHtml option as follows:

<appender name="SmtpAppender" type="log4net.Appender.ExtendedSmtpAppender">
    ...
    <isBodyHtml value="true" />
    ...
</appender>

The SendEmail method body source is taken from the latest version of the standard log4net's SmtpAppender. I only removed old Framework versions supporting stuff and added this string:

mailMessage.IsBodyHtml = IsBodyHtml;

You can get the latest version of the standard log4net's SmtpAppender here.

UPDATE:
Below is a sample config (inspired with these examples):

<log4net>
  <!-- SmtpExAppender is set to be our ExtendedSmtpAppender -->
  <appender name="SmtpExAppender" type="log4net.Appender.ExtendedSmtpAppender">
    <to value="to@domain.com" />
    <from value="from@domain.com" />
    <subject value="test logging message" />
    <isBodyHtml value="true" />
    <smtpHost value="SMTPServer.domain.com" />
    <bufferSize value="512" />
    <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>

  <!-- Set root logger level to DEBUG and its only appender to SmtpExAppender -->
  <root>
    <level value="DEBUG" />
    <appender-ref ref="SmtpExAppender" />
  </root>
</log4net>


文章来源: Log4net html formatted SmtpAppender