Reputation: 4559
I'm creating a console app which does importing of some data of different kinds. The type of data to import is specified by argument. For each type of import I created a logger and an appender with different filenames specified:
<logger name="ClientsImporter">
<level value="INFO" />
<appender-ref ref="ClientsImporter" />
</logger>
<logger name="PricesImporter">
<level value="INFO" />
<appender-ref ref="PricesImporter" />
</logger>
<appender name="ClientsImporter" type="log4net.Appender.RollingFileAppender">
<file value="..\logs\ClientsImporter_log" />
[...]
</appender>
<appender name="PricesImporter" type="log4net.Appender.RollingFileAppender">
<file value="..\logs\ImbalancePricesImporter_log" />
[...]
</appender>
<root>
<level value="INFO" />
<appender-ref ref="VisualStudioDebugOutput" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
In my code I have a class for each import type and this class instance of Logger created like this:
internal readonly ILog Log = LogManager.GetLogger("ClientsImporter");
The problem is that apparently log4net wants exclusive lock on all files for all appenders. Not only those used by current run of application. This is a problem because some imports can overlap witch each other (performed by different instances of same program) and then log4net throws error that it cannot get exclusive lock on log files (even those that will not be used by it).
Is there any way to tell log4net which loggers to use and ignore others?
Upvotes: 0
Views: 944
Reputation: 14962
In this answer I told the OP that log4net was usually greedy regarding its log files' creation:
the file is created as soon as the appender is initialized in the ActivateOptions() method. From what I see you need a custom FileAppender that will handle all your business rules. It would need to
- override the default ActivateOptions method to prevent file creation - override the Append method to create a new timestamped file when a message is logged - use data passed in the LoggingEvent class (you have some properties available on the class) to retrieve the filename; you have to determine the filename before logging.
Unless you want to benefit from behavior it already implements(impersonation for example), I'd recommend skipping inheritance from the FileAppender class and inherit directly from the TextWriterAppender class.
So I think that - unless you want to customize the behavior of the appenders - the fallback proposed by peer may be the most efficient option since it will avoid the exclusive locks on log files. You will still have logfiles that are created as soon as the configuration is parsed.
Upvotes: 1
Reputation: 27944
You can set the locking on the files to MinimalLock with lockingModel:
<appender name="ClientsImporter" type="log4net.Appender.RollingFileAppender">
<file value="..\logs\ClientsImporter_log" />
**<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>**
[...]
</appender>
Upvotes: 1