我试图将WriteTo.RollingFile与Serilog一起用作每天编写一个文件的如下方式:
var log = new LoggerConfiguration().WriteTo.RollingFile(
@"F:\logs\log-{Date}.txt",
LogEventLevel.Debug).CreateLogger();
log.Information("this is a log test");但是,在同一天,我将为每个日志条目获得一个新的日志文件!
如何将Serilog配置为每天写入一个新文件,以便每天有一个日志文件?
是否有任何归档过程删除超过7天的文件?
发布于 2015-08-20 01:55:01
试着在下面:
var log = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(@"f:\log\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();日志文件名将自动log-20150819.txt等。您不需要指定date.Old文件将按照retainedFileCountLimit清除-默认值为31。
发布于 2018-12-15 17:06:01
WriteTo.RollingFile被否决
由于发布的答案,方法RollingFile不再可行,取而代之的是File和滚动日志的选项。
现在必须使用支持滚动的标准Serilog.Sinks.File NuGet包:
.WriteTo.File(path: @"e:\logs\skilliam.log",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 123456);发布于 2018-01-04 20:46:59
这里有一种在web.config 4/5应用程序中使用Serilog的方法。
在web.config中添加以下内容:
<add key="serilog:minimum-level" value="Information" />
<add key="serilog:minimum-level:override:Microsoft" value="Information" />
<add key="serilog:minimum-level:override:System" value="Information" />
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<add key="serilog:write-to:RollingFile.pathFormat" value="./Logs/log-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />然后在Application_Start of global.asax中添加以下内容:
// Get application base directory
string basedir = AppDomain.CurrentDomain.BaseDirectory;
// Setup Serilog for logging
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.WriteTo.RollingFile(basedir + "/Logs/log-{Date}.txt")
.CreateLogger();https://stackoverflow.com/questions/32108148
复制相似问题