'SmtpPickupDirAppender' 不包含 'SmtpHost' 的定义并且没有可访问的扩展方法 'SmtpHost' 接受第一个参数

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

我在 .net framework class 库项目中有这段代码,我想重用它 in.net 标准 class 库项目。它按预期工作,但在 .net 标准项目中出现编译错误。

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

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 版本 2.0.8.

我在网上搜索过,但没有得到任何解决这个问题的线索,请帮忙。

我浏览了 log4net 官方网站,它目前不支持 .net 标准。

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

那么有什么解决方法可以解决这个问题吗?

我不明白你为什么说这个问题与 .net 标准有关。 SmtpPickupDirAppenderdocumentation 表示

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

此附加程序不适用于 Smtp 主机,但适用于文件系统目录。你需要设置 PickupDir 属性.

SmtpPickupDirAppender更改为SmtpAppender,以防您想发送真实的电子邮件或替换

smtpElement.SmtpHost = FX_CONNECT.EmailElement.Server;

来自

smtpElement.PickupDir = "C:\YourPickUpDir";

如果您想继续使用该附加程序。

感谢@Claudio Redi 的评论,它给了我解决问题的正确方向。

我将下面的 class 添加到我的项目中,并使用 SMTPAppender class 代替 var smtpElement = (SMTPAppender)AppLogger.Logger.Repository.GetAppenders().Where(appender => appender.Name.Equals(element.Name)).FirstOrDefault(); 行中的 log4net.Appender.SmtpPickupDirAppender 并且它的工作方式如下预期。

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());
        }
    }
}