Reputation: 2244
I have two programs: program1 and program2 (they are modules of biggest program and could be run simultaneously), which of them use log4net.dll. Log4net.dll is one and shared between both programs. Each program has its own log configuration. Configurations are only different by logging path.
First path:
"..\Logs\Program1\Log_%date{yyyyMMdd}.log"
Second path:
"..\Logs\Program2\Log_%date{yyyyMMdd}.log"
Here is an example of logging configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="..\Logs\Program1\Log_%date{yyyyMMdd}.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger � %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Everything works almost fine, but sometimes program2 writes log to program1 log file. It looks like log4net.dll cached program1 logging settings, and when program2 asked to log, framework uses the same settings. Please suggest, how can I resolve it?
Upvotes: 0
Views: 490
Reputation: 73243
In order for the two programs to write to the same file, you should define loggers for each in their config files.
So, instead of just defining a <root>
logger, define a specific logger for Program1 in its config, and the same for Program2 in its config:
<!-- program1.config -->
<appender name="Program1RollingFileAppender"
type="log4net.Appender.RollingFileAppender"> …
<logger name="Program1">
<level value="INFO" />
<appender-ref ref="Program1RollingFileAppender" />
</logger>
<!-- program2.config -->
<appender name="Program2RollingFileAppender"
type="log4net.Appender.RollingFileAppender"> …
<logger name="Program2">
<level value="INFO" />
<appender-ref ref="Program2RollingFileAppender" />
</logger>
Then, in each program, instantiate a logger with the same name, and it will pick up the correct appender:
public class Program1
{
ILog log = LogManager.GetLogger("Program1"); …
public class Program2
{
ILog log = LogManager.GetLogger("Program2"); …
Upvotes: 1