设置 Log4Net 每天只创建 4 个文件
Setting up Log4Net to create only 4 files per day
我希望每天有四个日志文件,文件名中包含日期。
类似于:
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
可以吗?
感谢@ChetanRanpariya 指点。
我不得不重写 RollingFileAppender,这是一个解决方案:
class RollingOverDaypartFileAppender : RollingFileAppender
{
private DateTime nextRolloverDate;
public RollingOverDaypartFileAppender(){}
protected override void AdjustFileBeforeAppend()
{
if (DateTime.Now.Hour % 6 != 0) return;
base.AdjustFileBeforeAppend();
}
}
我希望每天有四个日志文件,文件名中包含日期。 类似于:
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
可以吗?
感谢@ChetanRanpariya 指点。 我不得不重写 RollingFileAppender,这是一个解决方案:
class RollingOverDaypartFileAppender : RollingFileAppender
{
private DateTime nextRolloverDate;
public RollingOverDaypartFileAppender(){}
protected override void AdjustFileBeforeAppend()
{
if (DateTime.Now.Hour % 6 != 0) return;
base.AdjustFileBeforeAppend();
}
}