Search code examples
c#.netlog4net.net-standardlog4net-appender

'SmtpPickupDirAppender' does not contain a definition for 'SmtpHost' and no accessible extension method 'SmtpHost' accepting a first argument


I have this piece of code in .net framework class library project, I want to reuse it in.net standard class library project. It works as expected, but gives compilation error in .net standard project.

foreach (AppenderElement element in FX_CONNECT.EmailElement.Appenders)
{
    var smtpElement = (log4net.Appender.SmtpPickupDirAppender)AppLogger.Logger.Repository.GetAppenders().Where(appender => appender.Name.Equals(element.Name)).FirstOrDefault();
    if (smtpElement != null)
    {
        smtpElement.From = FX_CONNECT.EmailElement.From;
        smtpElement.To = FX_CONNECT.EmailElement.To;
        smtpElement.SmtpHost = FX_CONNECT.EmailElement.Server;
    }
}

Error for smtpElement.SmtpHost:

Error CS1061 'SmtpPickupDirAppender' does not contain a definition for 'SmtpHost' and no accessible extension method 'SmtpHost' accepting a first argument of type 'SmtpPickupDirAppender' could be found (are you missing a using directive or an assembly reference?)

log4net version in both application 2.0.8.

I searched on the internet but didn't get any clue how to solve this issue, please help.

I have gone through the log4net official site, It doesn't support .net standard as of now.

https://logging.apache.org/log4net/release/framework-support.html

So Is there any workaround to solve this?


Solution

  • Thank you @Claudio Redi for your comment, It gave me the right direction to solve the issue.

    I added the below class to my project and used the SMTPAppender class in place of log4net.Appender.SmtpPickupDirAppender in the line var smtpElement = (SMTPAppender)AppLogger.Logger.Repository.GetAppenders().Where(appender => appender.Name.Equals(element.Name)).FirstOrDefault(); and it's working as expected.

    using log4net.Appender;
    using log4net.Core;
    using System.IO;
    using System.Net.Mail;
    
    namespace FxCore.Diagnostics.Components
    {
        public class SMTPAppender : BufferingAppenderSkeleton
        {
            public string To { get; set; }
            public string From { get; set; }
            public string Subject { get; set; }
            public string SmtpHost { get; set; }
            public string Port { get; set; }
    
            protected void SendEmail(string messageBody)
            {
                SmtpClient client = new SmtpClient(SmtpHost);
                client.UseDefaultCredentials = false;
                client.Port = int.Parse(Port);
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress(From);
                    mailMessage.To.Add(To);
                    mailMessage.Body = messageBody;
                    mailMessage.Subject = Subject;
                    client.Send(mailMessage);
                }
            }
    
            protected override bool RequiresLayout => true;
    
            protected override void SendBuffer(LoggingEvent[] events)
            {
                StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
    
                string t = Layout.Header;
                if (t != null)
                {
                    writer.Write(t);
                }
    
                for (int i = 0; i < events.Length; i++)
                {
                    // Render the event and append the text to the buffer
                    RenderLoggingEvent(writer, events[i]);
                }
    
                t = Layout.Footer;
                if (t != null)
                {
                    writer.Write(t);
                }
    
                SendEmail(writer.ToString());
            }
        }
    }