Configuration of log4net … in class libraries!

When you’re using log4net to do your logging, you want to configure log4net at the earliest possible point in time.

I console- or winforms-applicartion this is easy to do, just add the appropriate code to your static void Main(). Even in web-applications this can be achieved quite easily, since you can make use of the global.asax and the Application_OnStart() event.

But how about a class library? Adding the code below:

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

to you AssemblyInfo.cs will make sure, that the log4net.config is being read as soon as the class library is access the first time.

Leave a Comment.