log4net 在 Release 模式下不起作用
🏷️ log4net
现象是在 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 callingXmlConfigurator.Configure()
directly fromApplication_Start()
. Not a pretty solution, but it seems to work.
代码
Web 项目
cs
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~") + @"\log4net.config"));
}
控制台项目
cs
static void Main()
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.config"));
}