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

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

                    <table>  <tr>

            for (int i = 0; i < loggingEvent.GetProperties().Count; i++)
              </tr>", loggingEvent.TimeStamp, loggingEvent.Properties["PackageId"], loggingEvent.RenderedMessage,
                    loggingEvent.Properties["BankCode"], loggingEvent.ThreadName,



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);
                if (!String.IsNullOrEmpty(Cc))
                if (!String.IsNullOrEmpty(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.'
                    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.

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

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


And my Root :

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