C# – Class project is not loading log4net.config from Main project

clog4net

I have my log4net Wrapper and log4net dll in its own class project and i have added the following to assemblyinfo.cs of the class project

// log4net config file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

So my main project has the log4net.config file and logging is working in my main project.

I also have another class project (nothing to do with log4net) which my main project (where logging is working) calls the class project. So i enter the new class project and need to log something but IsDebugEnabled returns false .. this is the line

      LogManager.GetLogger("DebugLogger").IsDebugEnabled;

So main project (webproject) which has a reference to my LOGGING class return TRUE, but a class project that i call from my main project returns false.

It appears that the class project isn't reading the log4net

Any ideas?

Best Answer

If I follow correctly, your setup is like this:

Web Project (web.config and log4net.config)

  • references Logging Wrapper
  • references Class Library Project

Logging wrapper

  • References log4net
  • Contains assembly:XmlConfigAttribute

Class Library Project

  • References log4net

Although you haven't posted any details on your log4net "wrapper" class, I'm assuming that your Web Project isn't accessing log4net directly, but through your wrapper.

As per my last answer, log4net is resolving the config and constructing the logging repository in your wrapper project. While I'm fairly certain that there's only one repository per AppDomain, it sounds like your Class Library Project isn't finding the default repository. Since it's at the AppDomain level, I doubt that it is exclusive to your wrapper Assembly directly.

Instead, this sounds like a timing issue. What happens if you call the wrapper first and then your class library? I suspect you'll find that if you call your Class Library Project first, you won't get a logger. However, if the very first logging call is to your wrapper, any subsequent requests for a logger should work. This is because the wrapper project contains the config setup information and that first call will configure the logging repository for the AppDomain.

While I'm not a huge fan of wrapping log4net, you can fix this issue by forcing a call to your wrapper in the application startup logic, or by having your Class Library project use your wrapper. Keep in mind that if you use the wrapper concept, your Class Library won't be able to leverage all the log4net built in features (like IsDebugEnabled) and you will have to resolve these settings through your wrapper.