我也面臨同樣的問題,推出每週日誌文件,通過測試GLM的解決方案的方法,在某種程度上它是不工作的版本的log4net的(1.2.15.0),但他的回答啓發,並研究源代碼,我做了一個解決方案,通過生成每週將日誌文件名(命名爲星期日的日期)
namespace log4net.Appender
{
class RollingOverWeekFileAppender : RollingFileAppender
{
public RollingOverWeekFileAppender()
{
IDateTime dt = new SundayDateTime();
DateTimeStrategy = dt;
}
class SundayDateTime : IDateTime
{
public DateTime Now
{
get { return CalcThisSunday(DateTime.Now); }
}
private DateTime CalcThisSunday(DateTime time)
{
// Calc this sunday
time = time.AddMilliseconds((double)-time.Millisecond);
time = time.AddSeconds((double)-time.Second);
time = time.AddMinutes((double)-time.Minute);
time = time.AddHours((double)-time.Hour);
return time.AddDays((double)(-(int)time.DayOfWeek));
}
}
}
}
而且我的log.config的片段
<appender name="Log" type="log4net.Appender.RollingOverWeekFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value=".\log-" />
</file>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<encoding value="utf-8" />
<staticLogFileName value="false"/>
<appendToFile value="true" />
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd.lo\g"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date	%level	%logger{1}	%message%newline" />
</layout>
</appender>