log4net 在 Release 模式下不起作用

Posted by 佳佳 on 2018-09-13 3 IT log4net C#

现象是在 Release 模式下生成的程序不会写日志,在 Debug 模式下生成的就可以正常记录日志。

Log4net works in Debug but fails in Release build 回答中说是 1.2.11 版中 Release 模式时的 条件编译符号 问题导致的,但是在我更新到最新的 2.0.8 版仍有该问题。
该问题的另一个回复却可以正确的解决该问题。


We experienced the same issues. It's still not clear to me what the root cause is, but we got log4net working again. We experienced the problems only in websites where we configure log4net using the XmlConfiguratorAttribute on the website assembly. We managed to work around this by calling XmlConfigurator.Configure() directly from Application_Start(). Not a pretty solution, but it seems to work.


代码

Web项目

protected void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~") + @"\log4net.config"));
}

控制台项目

static void Main()
{
    log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.config"));
}

参考

  1. Log4net works in Debug but fails in Release build
  2. log4net在release模式下无法生成文件或不写入日志
  3. og4net的release版本不记日志问题解决
  4. Download Apache log4net
  5. Apache log4net™ Manual - Configuration
  6. log4net - Appenders not working in IIS7.5
  7. log4net doesn't log when running a .Net 4.0 Windows application built in Release mode
版权声明:本文为博主原创文章,未经博主允许不得转载。http://www.liujiajia.me/blog/details/log4net-do-not-work-in-release-mode

Buy me a coffeeBuy me a coffee