Not sure what is going on here, but my registration code will not be written to the rollFileAppender file unless I call XmlConfigurator.Configure () for each call. I turned on debugging in the following code and I can confirm that when Configure is called only once in the constructor, it seems to pull my configuration, but when Log () is actually called, nothing happens with the log file OR appears in the debug window . If I uncomment this call to Configure () in Log () and allow it to be reconfigured on every call, then it works fine, but I don't think that is the intended use.
Bonus points! - I noticed that the very first call to log.Info () is not logged, but all subsequent ones in every process start are fine.
Thank!
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");
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>
source
share