'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?
I’m confused about why you say the problem is related to .net standard. The documentation of SmtpPickupDirAppender
says
/// The configuration for this appender is identical to that of the <c>SMTPAppender</c>, /// except that instead of specifying the <c>SMTPAppender.SMTPHost</c> you specify /// <see cref="PickupDir"/>.
This appender does not work with a Smtp host but with a file system directory. You need to set PickupDir
property.
Change SmtpPickupDirAppender
by SmtpAppender
in case you want to send emails for real or replace
smtpElement.SmtpHost = FX_CONNECT.EmailElement.Server;
by
smtpElement.PickupDir = "C:\YourPickUpDir";
in case you want to keep using that appender.
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()); } } }