【问题标题】:Does Log4Net haven an smtp appender that support TLS Encryption?Log4Net 是否有支持 TLS 加密的 smtp 附加程序?
【发布时间】:2018-05-12 09:49:59
【问题描述】:

如标题所述,我在查找声明它支持 TLS 加密标准的附加程序时遇到了一些问题。

我一直在尝试使用 SmtpAppender,但无法让它工作,并怀疑它与需要 TLS 加密的 smtp 服务器有关,而 SmtpAppender 可能不支持这一点。

setup from Office365 manual

The Manual

使用的配置:

<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>

【问题讨论】:

    标签: ssl log4net


    【解决方案1】:

    在使用最新的 Log4Net 1.2.11 版本进行一些额外研究和实际测试后答案是:是的。

    这个问题在C# ASP.NET Send Email via TLS 得到了一定程度的回答,并调查了 Log4Net allow smtp to ssl authenticate and with certificates. 的问题跟踪,据记录,Log4Net 有一个开关 EnableSsl,顾名思义,它启用 SSL 支持,第一个链接属于 TLS .

    【讨论】:

      【解决方案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 name="ErrorSmtpAppender" 
                type="SomeProject.Infrastructure.Logging.Log4netAppenders.SmtpCustomAppender">
      

      然后您可以将&lt;enablessl value="true" /&gt; 添加到您的日志附加程序中。

      我创建了一个gist 来向您展示它的外观。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多