正如标题中所述,我在寻找支持TLS加密标准的appender时遇到了一些问题。
我一直在尝试使用SmtpAppender,但无法让它工作,并怀疑它与需要TLS加密的smtp服务器有关,而且SmtpAppender可能不支持此功能。
使用的配置:
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="my email" />
<from value="The senders email" />
<Username value="JohnDoe"></Username>
<password value="NoWay" ></password>
<authentication value="Basic"></authentication>
<subject value="Test message" />
<smtpHost value="pod51011.outlook.com" />
<port value="587" />
<bufferSize value="512" />
<lossy value="true" />
<EnableSsl value="true"/>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="INFO"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
答案 0 :(得分:5)
经过一些额外的研究并使用最新的Log4Net版本1.2.11进行实际测试答案是:是的。
问题在这里有所回答C# ASP.NET Send Email via TLS并查看Log4Net allow smtp to ssl authenticate and with certificates. 的问题跟踪记录Log4Net有一个开关EnableSsl,正如名称所暗示的那样,启用SSL支持链接属于TLS。
答案 1 :(得分:2)
那些尚未准备升级到1.2.11的人的另一个选择是添加自定义appender:
/// <summary>
/// This is a custom appender so that we can enable SSL properly (and support TLS)
/// </summary>
public class SmtpCustomAppender : SmtpAppender
{
public bool EnableSsl { get; set; }
public SmtpCustomAppender()
{
Authentication = SmtpAuthentication.None;
Port = 25; //0x19;
//Port = 587; // 0x24b;
Priority = MailPriority.Normal;
EnableSsl = false;
}
/// <summary>
/// Send the email message - this overrides the email sender so that we can add enabling SSL
/// </summary>
/// <param name="messageBody">the body text to include in the mail</param>
protected override void SendEmail(string messageBody)
{
SmtpClient client = new SmtpClient();
if (!string.IsNullOrEmpty(SmtpHost))
{
client.Host = SmtpHost;
}
client.Port = Port;
client.EnableSsl = EnableSsl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
switch (Authentication)
{
case SmtpAuthentication.Basic:
client.Credentials = new NetworkCredential(Username, Password);
break;
case SmtpAuthentication.Ntlm:
client.Credentials = CredentialCache.DefaultNetworkCredentials;
break;
}
MailMessage message = new MailMessage
{
Body = messageBody,
From = new MailAddress(From)
};
message.To.Add(To);
message.Subject = Subject;
message.Priority = Priority;
client.Send(message);
}
}
然后在配置文件中,您只需要将以前的SmtpAppender设置为自定义appender的完全限定名称空间。
<appender name="ErrorSmtpAppender"
type="SomeProject.Infrastructure.Logging.Log4netAppenders.SmtpCustomAppender">
然后,您可以将<enablessl value="true" />
添加到日志追加器中。
我创建了一个gist来向您展示这个样子。