Reputation: 799
Edit: I take it back, I had an error in my code, once I fixed it, it works. The code is now updated to the working version.
The error was in ThreadContext.Properties[KEY_LOG_FOLDER] = loggerName;
and
ThreadContext.Properties[KEY_LOG_FILE] = loggerName;
. I accidentally put "KEY_LOG_FOLDER"
and "KEY_LOG_FILE"
in the quotes, and they should have been const strings defined elsewhere.
I'm trying to have a rolling file appender per connection. The connections dynamically come in and cannot be determined at start up. At the moment I'm trying the following:
XML:
<log4net>
<logger name="Connection.CommunicationLogger">
<appender-ref ref="ConnectionCommunicationFileAppender"/>
<level value="ALL"/>
</logger>
<appender name="ConnectionCommunicationFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\logs\%property{LogFolder}\%property{LogName}.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date: %message%newline" />
</layout>
</appender>
</log4net>
Code:
public FileLogger(string loggerName)
{
string logsDir = ConfigurationManager.AppSettings["LogRootDirectory"];
System.IO.Directory.CreateDirectory(logsDir + "\\" + loggerName);
ILoggerRepository loggerRepository = LogManager.CreateRepository(loggerName + REPOSITORY);
ThreadContext.Properties[KEY_LOG_FOLDER] = loggerName;
ThreadContext.Properties[KEY_LOG_FILE] = loggerName;
log4net.Config.XmlConfigurator.Configure(loggerRepository);
logger = LogManager.GetLogger(loggerName + REPOSITORY, "Connection.CommunicationLogger");
}
This however does not seem to work when it's all executing quickly.
Should I programmatically create appenders per connection, or would that be bad practice?
Upvotes: 0
Views: 33
Reputation: 27944
I belief the filename is only set once, so not on every time you are logging. If you want different filename, an option can be to make different loggers for each file dynamically as you propose in you your question.
Upvotes: 2