Help Override PatternLayout and Smtpappender

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help Override PatternLayout and Smtpappender

alicankablan
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hello everyone I am happy to find this forum.I have some problems about SMTP and Layout (LOG4NET)

My problem is I override SmtpAppender (the reason is it doesnt support HTML) and PatternLayout without override SmtpAppender it's work the mails is coming.But when I override my SmtpAppender noting happens.Can you see any mistake here ?

Pattern Override (I think I have problems at first in loggingEvent.GetProperties().Count  ):

public override void Format(TextWriter writer, LoggingEvent loggingEvent)
        {
            StringBuilder str = new StringBuilder(@"<!DOCTYPE html>
                    <html>
                    <head>
                    <style>
                    table {
                        font-family: arial, sans-serif;
                        border-collapse: collapse;
                        width: 100%;
                    }

                    td, th {
                        border: 1px solid #dddddd;
                        text-align: left;
                        padding: 8px;
                    }

                    tr:nth-child(even) {
                        background-color: #dddddd;
                    }
                    </style>
                    </head>
                    <body>

                    <table>  <tr>
                    <th>LogDate</th>
                    <th>PaketNo</th>
                    <th>Hata</th>
                    <th>BankaKodu</th>
                     <th>Thread</th>
                  </tr>");

            for (int i = 0; i < loggingEvent.GetProperties().Count; i++)
            {
                str.AppendFormat(@"<tr>
                <td>{0}</td>
                <td>{1}</td>
                <td>{2}</td>
                <td>{3}</td>
                    <td>{4}</td>
                <td>{5}</td>
              </tr>", loggingEvent.TimeStamp, loggingEvent.Properties["PackageId"], loggingEvent.RenderedMessage,
                    loggingEvent.Properties["BankCode"], loggingEvent.ThreadName,
                    loggingEvent.ExceptionObject.Message);
            }
            str.Append(@"</table>

            </body>
            </html>");

            writer.Write(str.ToString());
           
        }

SmtpAppender Override :

protected override void SendEmail(string messageBody)
        {
            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 = true;
                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'
                    mailMessage.ReplyTo = new MailAddress(ReplyTo);

                }
                mailMessage.Subject = Subject;
                //mailMessage.SubjectEncoding = m_subjectEncoding;
                mailMessage.Priority = Priority;

                // behaviour compared to .NET 1.x. We would need a SendCompletedCallback to log errors.
                smtpClient.Send(mailMessage);
            }
        }

My config (I need only ERROR message in SMTP):

   <logger additivity="false" name="WinService">
      <level value="INFO"/>
      <appender-ref ref="WinServiceAppender" />
	  <appender-ref ref="SMTPAppender" />
	 
    </logger>

  <appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
    <to value="xxxx@xxx.com" />
     <from value="xxxx@xxx.com" />
     
     
      <subject value="Error Info -- ( Win Service)" />
      <smtpHost value="xxxx" />
      <port value="xxxx" />
      
<filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
    <layout type="EDistraintDataAccess.Helper.EhacizSmtpLayout" />

    </appender>

And my Root :

 <root>
      <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="SMTPAppender" />
      <appender-ref ref="AdoNetAppender" />
    </root>
Loading...