简体   繁体   English

Log4Net单独的配置文件无法正常工作

[英]Log4Net separate config file not working

I'm having a strange problem. 我有一个奇怪的问题。 I have multiple projects in a solution. 我在解决方案中有多个项目。 One of them is a WebAPI and it logs just fine. 其中一个是WebAPI,它记录得很好。 Another is an MVC admin site, this one won't log. 另一个是MVC管理站点,这个不会登录。 Here's what I've tried. 这是我尝试过的。

My logging code: 我的日志代码:

var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Some log message");  

In the assemblyInfo I tried each of the following... 在assemblyInfo中,我尝试了以下各项......

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "logging.config", Watch = true)]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "C:\\full_path\\logging.config", Watch = true)]  

In the Global.asax application_start I've tried the following (removing the assemblyInfo lines) 在Global.asax application_start中我尝试了以下(删除assemblyInfo行)

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Logging.config"));
var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Application starting");  

This also doesn't create a log. 这也不会创建日志。 The one way I could get one created was to use the code in the global.asax and the full path to the logging.config. 我可以创建一个的方法是使用global.asax中的代码和logging.config的完整路径。

I'm confused, since the webapi works with just the assemblyInfo with only a relative path. 我很困惑,因为webapi只使用只有相对路径的assemblyInfo。 I also made a sample dummy solution with an MVC project with the AssemblyInfo solution (relative path), and it worked there too. 我还使用AssemblyInfo解决方案(相对路径)制作了带有MVC项目的示例虚拟解决方案,并且它也在那里工作。

Any ideas? 有任何想法吗?

UPDATE - FOUND ISSUE 更新 - 发现问题

I had to setup the config file watcher in Application_Start: 我必须在Application_Start中设置配置文件观察器:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "logging.config"));

Turns out that DotNetOpenAuth.Core.dll uses log4net and will make using the AssemblyInfo solution not work. 事实证明,DotNetOpenAuth.Core.dll使用log4net,并且使用AssemblyInfo解决方案无法正常工作。 I found the details in this blog: http://hanskindberg.wordpress.com/2013/04/07/log4net-configuration/ 我在这个博客中找到了详细信息: http//hanskindberg.wordpress.com/2013/04/07/log4net-configuration/

Thanks 谢谢

I had to setup the config file watcher in Application_Start: 我必须在Application_Start中设置配置文件观察器:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "logging.config"));

Turns out that DotNetOpenAuth.Core.dll uses log4net and will make using the AssemblyInfo solution not work. 事实证明,DotNetOpenAuth.Core.dll使用log4net,并且使用AssemblyInfo解决方案无法正常工作。 I found the details in this blog: http://hanskindberg.wordpress.com/2013/04/07/log4net-configuration/ (look at the paragraph that mentions DotNetOpenAuth.Core.dll) 我在这个博客中找到了详细信息: http ://hanskindberg.wordpress.com/2013/04/07/log4net-configuration/(查看提到DotNetOpenAuth.Core.dll的段落)

I struggled for this solution for many days. 多年来我一直在努力寻找这个解决方案。 I could finally found below solution worked for me> 我终于可以找到以下解决方案为我工作>

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] plus the configuration suggested by @BCarlson [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]加上@BCarlson建议的配置

XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

To summaries everything, I have created a sample c# console project for log4net configuration with external log4net.config file. 为了总结一切,我已经使用外部log4net.config文件为log4net配置创建了一个示例c#控制台项目。 https://github.com/riddhik84/Log4net-Tutorial https://github.com/riddhik84/Log4net-Tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM