I am a Log4Net newbie and trying to get a basic/safe understanding of how it works. If I configure my Logger to a FileAppender, and I have multiple statements like below, one after the other:
this.GetLogger().Info("...");
this.GetLogger().Error("....");
Does each call actually open the file, write string and closes it? Every time? Or is there anything else that goes on? I want to know when the file resource is in use. How does it work?
The docs:
In other words: it will try to open the file as early as possible so no extra overhead occurs whenever you’re trying to log. If that fails, it will attempt to open the file every time you try to log anything.
You can easily check how logging behaves in your specific instance – whenever the file is opened, the layout’s Header value will be written to the file, whenever it’s closed the layout’s Footer value will be written.
Note, however, that this is the default behavior.
FileAppenderuses theFileAppender.ExclusiveLocklocking model by default. Another option is theFileAppender.MinimalLocklocking model, which tries to acquire the lock before each logging operation and releasing it afterwards. You can configure your appender as follows to use it.Acquiring the lock on each and every logging operation is obviously more time-consuming than the default “acquire once, release once” model. There are valid reasons for doing so, though – for example if the logfile needs to be rotated during excecution of a long-running application.