Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net SmtpAppender not working in .netCore

Tags:

I'm trying to send log messages to my gmail account but it keeps not working. I've tried changing ports and changing threshold value but it's not working. When I try writing logs in file everything is ok, so I think the problem is in log4net.config file.

My log4net configuration file contains:

    <log4net>
     <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="[email protected]" />
      <from value="[email protected]" />
      <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>

In my Startup.cs file I have:

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

And my example code is:

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

Am I making something wrong. I tried everything

like image 944
Grizabela Avatar asked Apr 20 '17 11:04

Grizabela


1 Answers

log4net's SmtpAppender is not supported in .net core yet.

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

like image 112
Jay Shah Avatar answered Sep 25 '22 10:09

Jay Shah