Log4Net SmtpAppender无法在.netCore中运行

时间:2017-04-20 11:26:17

标签: asp.net-core log4net

我正在尝试将日志消息发送到我的Gmail帐户,但它仍无法正常工作。我已经尝试更改端口和更改阈值,但它不起作用。当我尝试在文件中写入日志时一切正常,所以我认为问题出在log4net.config文件中。

我的log4net配置文件包含:

    <log4net>
     <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="mymail@gmail.com" />
      <from value="mysendermail@gmail.com" />
      <subject value="Crash log" />
      <smtpHost value="smtp.gmail.com" />
      <authentication value="Basic" />
      <port value="465" />
      <username value="myusername" />
      <password value="mypass" />
      <bufferSize value="1" />
      <EnableSsl value="true"/>
      <lossy value="true" />
     <evaluator type="log4net.Core.LevelEvaluator">
       <threshold value="DEBUG"/>
     </evaluator>
     <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
     </layout>
   </appender>
   <root>
     <level value="DEBUG"/>
     <appender-ref ref="SmtpAppender"/>
   </root>
   </log4net>

在我的Startup.cs文件中,我有:

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

我的示例代码是:

public class HomeController : Controller
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(HomeController));

        public HomeController()
        {

        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {

            ViewData["Message"] = "Your application description page.";
            ViewData["Title"] = "title";
            log.Warn("test test", new NullReferenceException("missing"));
            log.Debug("test test", new NullReferenceException("missing"));
            log.Error("test test", new NullReferenceException("missing"));
            log.Fatal("test test", new NullReferenceException("missing"));
            return View();
        }
    }

我做错了什么。我尝试了一切

2 个答案:

答案 0 :(得分:1)

log4net的.net核心尚不支持SmtpAppender。

支持的框架:https://logging.apache.org/log4net/release/framework-support.html

答案 1 :(得分:0)

您是否测试过以下端口:

Gmail SMTP端口(TLS):587

Gmail SMTP端口(SSL):465&lt; - 您当前的设置

what-are-the-gmail-smtp-settings