I have been using log4net for a year and a half now and there are scenarios where I'm expecting it to log some data only to find out that it's not logging. I know two possible reasons of log4net not logging:
1. It is not yet configured to log.
I am configuring log4net through the XML configuration files of .NET (not programmatically) and I always forget to include the
[assembly: XmlConfigurator(Watch = true)]
line in the assembly containing the starting entry of the application (windows application project or the web application project and not the class libraries).
2. The first call to LogManager.GetLogger was not coming from the assembly containing the log4net configurations.
Normally, .NET applications are structured with Data, Core, Presentation, Web assemblies. log4net configurations are specified in the web.config of the Web project. If the first call to LogManager.GetLogger was coming not from the Web assembly (Core, Data, or Presentation), you probably wouldn't get any logs. From the log4net documentation "When should I log my first message":
The simple answer is as soon as possible. The long answer is more complex.
If you are configuring log4net programmatically, i.e. by calling the XmlConfigurator.Configure method then you should do so before you begin logging and it is reasonable to do this very soon after application start.
If you are configuring log4net by specifying assembly level attributes on your assembly then the configuration will be loaded once the first call to the LogManager.GetLogger is made. It is necessary that the first call to LogManager.GetLogger made during the process (or AppDomain) is made from the assembly that has the configuration attributes. Log4net will look only once and only on the first calling assembly for the configuration attributes.
Posted
Jul 05 2007, 03:32 PM
by
jokiz