Not sure whats going on here, but my logging code will not write to the rollingFileAppender unless I call XmlConfigurator.Configure() on every call. I have turned on debugging in the following code and can confirm that when Configure is called just the one time in the constructor it does appear to be pulling in my configuration, but when Log() is actually called, nothing happens to the log file OR shows up in the debug window.
If I uncomment that call to Configure() in Log() and let it reconfigure on every call, then it works just fine, but I don’t think that is intended usage.
Bonus Points! – I notice that the very first call to log.Info() is not logging, but all subsequent ones in each run of the process are fine.
Thanks!
public static class LogToFile
{
public const string RollingFileAppenderName = "RollingFileLogger";
static LogToFile()
{
log4net.Config.XmlConfigurator.Configure();
}
public static void Log(string fileNameBase, string message, string context)
{
if (fileNameBase == null) throw new ArgumentNullException("fileNameBase");
if (message == null) throw new ArgumentNullException("message");
if (context == null) throw new ArgumentNullException("context");
//log4net.Config.XmlConfigurator.Configure();
string fileName = string.Format("{0}_{1}.log", fileNameBase, context);
string fullFileName = Path.Combine(Properties.Settings.Default.LogFilePath, fileName);
if (!Directory.Exists(Properties.Settings.Default.LogFilePath)) Directory.CreateDirectory(Properties.Settings.Default.LogFilePath);
LogicalThreadContext.Properties["LogName"] = string.Format(fullFileName);
ILog log = LogManager.GetLogger(RollingFileAppenderName);
log.Info(message);
}
}
<appender name="rollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogName}" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<countDirection value="-1"/>
<LockingModel value="log4net.Appender.FileAppender+MinimalLock"/>
<staticLogFileName value="false" />
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="AlertMessageHandler Message : %-25date [%thread] - %newline%message%newline" />
</layout>
</appender>
Actuall sgmoore’s advice led me to the solution. If I can just name the log file on the Appender object instead of messsing with the config file Property, then calling ActivateOptions works just fine. Here’s the working code. Thanks!