10

I'm having a strange problem. I have multiple projects in a solution. One of them is a WebAPI and it logs just fine. Another is an MVC admin site, this one won't log. 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...

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

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.

I'm confused, since the webapi works with just the assemblyInfo with only a relative path. I also made a sample dummy solution with an MVC project with the AssemblyInfo solution (relative path), and it worked there too.

Any ideas?

UPDATE - FOUND ISSUE

I had to setup the config file watcher in 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. I found the details in this blog: http://hanskindberg.wordpress.com/2013/04/07/log4net-configuration/

Thanks

BCarlson
  • 1,122
  • 2
  • 11
  • 26
  • Are there permission issues (running it under IIS and trying to write to a file could has issues)? Have you tried [debugging](http://stackoverflow.com/q/677640/724591) it? Have you tried using the full path in the sample application_start code? Does your configuration have a root element, or does it match up with whatever `DeclaryingType` prints out? – matth Jan 17 '14 at 15:39
  • Permission issue: I don't think so, since running it in the global.asax works with full path. – BCarlson Jan 17 '14 at 16:30
  • Debugging: In VS2012, it just executes with no error when I log. Log4Net debugging, I haven't been able to get it to log anything. It's confusing that the same config works or doen't work based upon relative/full path and global vs assemblyInfo. – BCarlson Jan 17 '14 at 16:40
  • Found the problem. My MVC project has DotNetOpenAuth.Core.dll referenced. This assembly uses Log4Net, and is called before the AssemblyInfo line is executed. I found this article that gave me the details: http://hanskindberg.wordpress.com/2013/04/07/log4net-configuration/ – BCarlson Jan 21 '14 at 16:36

2 Answers2

13

I had to setup the config file watcher in 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. 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)

BCarlson
  • 1,122
  • 2
  • 11
  • 26
3

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

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. https://github.com/riddhik84/Log4net-Tutorial

Riddhi
  • 61
  • 1
  • 7