7

複数の smtp サーバーのホスト アドレスを指定し、1 つの smtp サーバーを使用した電子メールが失敗した場合に次の smtp サーバー アドレスを使用して送信を試みるロジックを実装できるようにしたいと考えています。log4net を使用することは可能ですか。log4net のいくつかの機能をオーバーライドして、独自のロジックを実装してメールを送信できますか?

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="group@ivp.in" />
  <from value="uname@ivp.in" />
  <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>
4

3 に答える 3

47

もっと簡単な方法があります

<to value="group@ivp.in,group2@ivp.in,group3@ivp.in,group4@ivp.in" />
于 2012-07-17T08:02:21.990 に答える
8

実装した場合のみ可能ですcustom SmtpAppender

Log4net ソースコードの例CustomSmtpAppenderからコードをコピーしました。うまくいけば、これはあなたを助けるでしょう.

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;
    }
}
于 2012-06-07T14:24:56.333 に答える