2012-06-07 73 views
4

我希望能夠指定多個smtp服務器主機地址並實現邏輯,如果使用一個smtp服務器的電子郵件失敗,它會嘗試使用下一個smtp服務器地址發送。有沒有可能使用log4net。我們可以重寫log4net的一些功能並實現我們自己的邏輯來發送電子郵件嗎?在log4net中使用smtpAppender的多個smtpost地址

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> 
    <to value="[email protected]" /> 
    <from value="[email protected]" /> 
    <subject>Error Notification</subject> 
    **<smtpHost value="10.0.0.12, 10.0.0.13" />** 
    <authentication value="None" /> 
    <port value="25" /> 
    <bufferSize value="1" /> 
    <EnableSsl value="false"/> 
    <lossy value="true" /> 
    <evaluator type="log4net.Core.LevelEvaluator"> 
    <threshold value="ERROR"/> 
    </evaluator> 
    <layout type="log4net.Layout.PatternLayout"> 
    <conversionPattern value="%newline%date [%thread] %level %logger - %message%newline%newline%exception" /> 
    </layout> 
</appender> 

回答

6

只有實施custom SmtpAppender纔有可能。

我已經複製CustomSmtpAppender代碼Log4net sourcecode Examples. hopefullly這會幫助你。

不要忘記參考CustomSmtpAppender在您的應用程序配置

using System; 
using System.IO; 
using System.Web.Mail; 

using log4net.Layout; 
using log4net.Core; 
using log4net.Appender; 

namespace SampleAppendersApp.Appender 
{ 
    /// <summary> 
    /// Simple mail appender that sends individual messages 
    /// </summary> 
    /// <remarks> 
    /// This SimpleSmtpAppender sends each LoggingEvent received as a 
    /// separate mail message. 
    /// The mail subject line can be specified using a pattern layout. 
    /// </remarks> 
    public class SimpleSmtpAppender : AppenderSkeleton 
    { 
     public SimpleSmtpAppender() 
     { 
     } 

     public string To 
     { 
      get { return m_to; } 
      set { m_to = value; } 
     } 

     public string From 
     { 
      get { return m_from; } 
      set { m_from = value; } 
     } 

     public PatternLayout Subject 
     { 
      get { return m_subjectLayout; } 
      set { m_subjectLayout = value; } 
     } 

     public string SmtpHost 
     { 
      get { return m_smtpHost; } 
      set { m_smtpHost = value; } 
     } 

     #region Override implementation of AppenderSkeleton 

     override protected void Append(LoggingEvent loggingEvent) 
     { 
      try 
      {  
       StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); 

       string t = Layout.Header; 
       if (t != null) 
       { 
        writer.Write(t); 
       } 

       // Render the event and append the text to the buffer 
       RenderLoggingEvent(writer, loggingEvent); 

       t = Layout.Footer; 
       if (t != null) 
       { 
        writer.Write(t); 
       } 

       MailMessage mailMessage = new MailMessage(); 
       mailMessage.Body = writer.ToString(); 
       mailMessage.From = m_from; 
       mailMessage.To = m_to; 

       if (m_subjectLayout == null) 
       { 
        mailMessage.Subject = "Missing Subject Layout"; 
       } 
       else 
       { 
        StringWriter subjectWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture); 
        m_subjectLayout.Format(subjectWriter, loggingEvent); 
        mailMessage.Subject = subjectWriter.ToString(); 
       } 

       if (m_smtpHost != null && m_smtpHost.Length > 0) 
       { 
        SmtpMail.SmtpServer = m_smtpHost; 
       } 

       SmtpMail.Send(mailMessage); 
      } 
      catch(Exception e) 
      { 
       ErrorHandler.Error("Error occurred while sending e-mail notification.", e); 
      }  
     } 

     override protected bool RequiresLayout 
     { 
      get { return true; } 
     } 

     #endregion // Override implementation of AppenderSkeleton 

     private string m_to; 
     private string m_from; 
     private PatternLayout m_subjectLayout; 
     private string m_smtpHost; 
    } 
} 
+0

完美!只是我在找什麼。現在我可以按照我想要的方式實現自己的smtp處理程序。謝謝 –

34

還有一個更簡單的方法

<to value="[email protected],[email protected],[email protected],[email protected]" /> 
+0

問題是'我想能夠指定多個smtp服務器主機地址',而不是多個'to'地址 – Geoff

+6

好吧。但我的回答是我對某些用戶有用的答案:) –

+4

這可能不是op問題的答案,但它是我在登陸此處時搜索的答案。 18 upvotes vs 4說明這裏的大多數人都在同一條船上 – buddybubble

相關問題