问题
I used this config but a date is always added to the current file ('log.20130805.0.log').
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/logFile.log" />
<appendToFile value="true" />
<preserveLogFileNameExtension value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd" />
<maximumFileSize value="10MB" />
<countDirection value="1"/>
<maxSizeRollBackups value="-1" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
The result of that config is :
log.20130805.0.log
log.20130805.1.log
log.20130805.2.log
log.20130805.3.log
What I get with staticLogFileName = true is :
log.log
log.1.log
log.2.log
log.3.log
What I want is :
log.log
log.20130805.1.log
log.20130805.2.log
log.20130805.3.log
回答1:
You can use the function below. This function first gets the file location that you set in web.config and after that you can add any path that you want! (like Date or Customer or...)
WebConfig:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\t4\\"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="_yyyy-MM-dd.lo'g'"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date User:%identity IP:%X{addr} Browser: %X{browser} Url: %X{url} [%thread] %-5level %c:%m%n"/>
</layout>
</appender>
Function:
public static void ChangeFileLocation(string _CustomerName,string _Project)
{
XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h =(log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
foreach (IAppender a in h.Root.Appenders)
{
if (a is FileAppender)
{
FileAppender fa = (FileAppender)a;
string sNowDate= DateTime.Now.ToLongDateString();
// Programmatically set this to the desired location here
string FileLocationinWebConfig = fa.File;
string logFileLocation = FileLocationinWebConfig + _Project + "\\" + CustomerName + "\\" + sNowDate + ".log";
fa.File = logFileLocation;
fa.ActivateOptions();
break;
}
}
}
and result is like this: C:\t4\TestProject\Customer1\Saturday, August 31, 2013.log
回答2:
Based on these tips I guess it isn't possible, unfortunately (with the current implementation of RollingFileAppender):
http://geekswithblogs.net/rgupta/archive/2009/03/03/tips-on-using-log4net-rollingfileappender.aspx
来源:https://stackoverflow.com/questions/18063073/how-to-configure-static-log-file-name-with-compositely-named-roll-backups