When logging with log4net to a file (using the FileAppender
), the FileAppender
is holding an exclusive write lock on the file. This doesn’t cause any problems, not even when the application is running with multiple threads, because
log4net should be thread-safe.
This does change however when working with multiple processes, that all share a common log4net configuration and
thus all will utilize the same FileAppender
.
This prevents other processes from writing to the file and usually the error "The
process cannot access the file 'xxx.log' because it is being used by another process." will show. In this
case there should not be an exclusive write lock by any process. Fortunately log4net has an appropriate configuration-setting, then all processes will write to the same log file.
1
|
< lockingModel type = "log4net.Appender.FileAppender+MinimalLock" />
[...] </ appender > |
But acquiring and release locks is quite costly and thus will slow down the overall performance of the application.
An alternative would be to create an individual log per processes. Luckily log4net supports the expansion of variables to generate log-filenames, so we can add the process-id to the filename-pattern.
1
2
3
4
|
< appender name = "LogFileAppender" type = "log4net.Appender.RollingFileAppender,log4net" > < file type = "log4net.Util.PatternString" value = "Log[%processid]" /> [...] </ appender > |