由于 log4net 到目前为止不支持消息正文中的 HTML 格式,我编写了自己的附加程序:
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);
}
}
}
}
要在 log4net 配置中使用此附加程序,只需替换标准 SmtpAppender 定义的命名空间和类名并添加 isBodyHtml 选项,如下所示:
<appender name="SmtpAppender" type="log4net.Appender.ExtendedSmtpAppender">
...
<isBodyHtml value="true" />
...
</appender>
SendEmail 方法体源取自最新版本的标准 log4net 的 SmtpAppender。我只删除了旧的框架版本支持的东西并添加了这个字符串:
mailMessage.IsBodyHtml = IsBodyHtml;
您可以在此处获取标准 log4net 的SmtpAppender
最新版本。
更新:
下面是一个示例配置(受这些示例的启发):
<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>