如何在serilog日志文件名中使用属性值



如果我有以下代码:

.WriteTo.Logger(c =>
c.Filter.ByIncludingOnly(Matching.WithProperty("tsBatchStarted"))
.WriteTo.File(
path: Path.Combine(
baseDir,
"App_Data",
"logs",
$"SERCrawlerLog_{tsBatchStarted}.txt")
, restrictedToMinimumLevel: LogEventLevel.Information
, outputTemplate: logTextTemplate
//, rollingInterval: RollingInterval.Day
, retainedFileCountLimit: 2
, shared: true
) // end .WriteTo.Logger(c =>

我希望在文件名中包含tsBatchStarted的属性值。。。

如何在序列日志的日志文件名中使用事件的属性值

dotnet add package Serilog.Sinks.Map

然后:

.WriteTo.Logger(c => c
.Filter.ByIncludingOnly(Matching.WithProperty("tsBatchStarted"))
.WriteTo.Map(
"tsBatchStarted",
"none",
(tsBatchStarted, wt) => wt.File(
path: Path.Combine(
baseDir,
"App_Data",
"logs",
$"SERCrawlerLog_{tsBatchStarted}.txt"),
restrictedToMinimumLevel: LogEventLevel.Information,
outputTemplate: logTextTemplate,
shared: true
),
sinkMapCountLimit: 2
)
) // end .WriteTo.Logger(c =>

请注意,这不会导致文件滚动,您需要使用基于应用程序的机制来清理旧批中的日志。

相关内容

最新更新