log4net is not logging - one uncommon reason

In the web, one can find many people asking, why their log4net configuration is not logging. Usually, they forgot to call XmlConfigurator.Configure();


But in my case, the problem was a different: 

  • I specify my logging configuration at runtime via code
  • it works sometimes, sometimes it does not work
  • no exceptions happened
  • it works, when i log to the EventLog, but only works sometimes when I log to a file


The reason: the log file is locked

I have two processes (a Windows Service and a Configuration UI) which share the logging configuration/storage (the UI logs config changes, the server logs requests it executes). So, if the FileAppender is used, it locks the file and the second process can not log to the file anymore. And since log4net is a fail-stop system, it just swallows this information. Depending on which process started first (the UI or the server) the latter could not log anymore until the other was terminated.


To find out such internal exceptions, turn on Internal Debugging of log4net: You've Been Haacked shows how. This helped tremendously...

The solution: either split the log store into two or use FileAppender.MinimalLock

Since the two processes are working together, I want to have one log store.

For the file-based logging, specify the FileAppender.MinimalLock to instruct log4net to not exclusively lock the file the whole time (this has performance issues obviously though)

Posted on August 7, 2014 and filed under development.