Logger.cs:
using System.Configuration;
using System.Threading;
using System;
using Serilog;
using Serilog.Enrichers;
using Serilog.Events;
using Serilog.Sinks.RollingFile;
namespace my.package.Logger
{
public static class Logger
{
static Logger() {
Log.Logger = new LoggerConfiguration().Enrich.WithThreadId().ReadFrom.AppSettings().CreateLogger();
}
}
}
App.config中:
<appSettings>
<add key="serilog:write-to:RollingFile.pathFormat" value="path\to\LOG{Date}.txt" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="2" />
<add key="serilog:write-to:RollingFile.fileSizeLimitBytes" value="" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm.ss,fff} {Level} [{ThreadId}]: {Message}{NewLine}{Exception}" />
<add key="serilog:minimum-level" value="Debug" />
</appSettings>
但是尽管retainFileCountLimit设置为2,日志文件也会累积(今天有6个)。
有趣的是,如果我重新启动系统(不是服务,整个系统),文件会被正确清理,只保留最后2个文件。
有什么想法吗?