There are plenty of possibilities to integrate log4net logging into your application. You can declare log4net configuration declaratively or imperatively for the whole project, in each separate assembly or even in each separate class. The pattern I describe below assumes that:
- you want to have a single and possibly the simplest configuration of the log4net for your whole application
- you do not want to call explicitely any complicated initialization code
- you do not want to create loggers explicitely (people tend to do that however it’s rather cumbersome)
The idea is as follows – you create a simple proxy class for the logging purposes. The class will have a static constructor responsible for configuring the log4net and two proxy methods for actual logging. It’s up to you to declare this class in the of your assemblies which is then referenced by other solution assemblies.
From now on the only way you log anything is to use the proxy class:
Then, in the application’s configuration file you provide a single configuration for log4net. I usually delegate the configuration to a separate file so that the web.config/app.config contains only:
and a separate file, located under \Configuration\log4net.config contains the actual configuration:
(this is only an example configuration)
That’s all. No other code is required to either configure log4net itself or create logging objects. You’ll find an example, consisting of a web application and an auxiliary assembly here.