我想每天有四个日志文件,其中日期作为文件名的一部分。 像这样:
MYAPP-20190108-4.log
MYAPP-20190108-3.log
MYAPP-20190108-2.log
MYAPP-20190108-1.log
MYAPP-20190107-4.log
MYAPP-20190107-3.log
MYAPP-20190107-2.log
MYAPP-20190107-1.log
我知道我可以利用日期模式,但是最终我得到了24个文件...
<Logging Message2FilePath="C:\Messages">
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="RollingStyle" value="Composite" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="false" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="10MB" />
<file type="log4net.Util.PatternString" value="C:\Logs\MYAPP-" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yy HH:mm:ss,fff} [%-5p][%3t]%m%n" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<datePattern value="yyyyMMdd-HH'.log'" />
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yy HH:mm:ss,fff} [%-5p][%3t]%m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</Logging>
键设置:
<param name="RollingStyle" value="Composite" />
<file type="log4net.Util.PatternString" value="C:\Logs\MYAPP-" />
<datePattern value="yyyyMMdd-HH'.log'" />
结果输出:
MYAPP-20190107-13.log
MYAPP-20190107-12.log
MYAPP-20190107-11.log
MYAPP-20190107-19.log
MYAPP-20190107-09.log
有可能吗?
答案 0 :(得分:1)
Thanks for the pointer @ChetanRanpariya. I had to override RollingFileAppender, here is a solution:
class RollingOverDaypartFileAppender : RollingFileAppender
{
private DateTime nextRolloverDate;
public RollingOverDaypartFileAppender(){}
protected override void AdjustFileBeforeAppend()
{
if (DateTime.Now.Hour % 6 != 0) return;
base.AdjustFileBeforeAppend();
}
}