2017-04-20 99 views
2

我想發送日誌消息到我的gmail帳戶,但它不工作。我試過更改端口並更改閾值,但它不起作用。當我嘗試在文件中寫入日誌時,一切正常,所以我認爲問題出在log4net.config文件中。Log4Net SmtpAppender不工作在.netCore

我log4net的配置文件中包含:

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

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

難道我做的東西錯誤。我試圖

+0

可能的複製https://stackoverflow.com/questions/15523613/log4net-smtp-appender-not-sending-emails – sgmoore

+0

我也有同樣的問題。當我檢查時,發現.net核心的log4net庫沒有log4net.Appender.SmtpAppender類。 –

回答