简体   繁体   English

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

[英]How do I use a property value in serilog log file name?

If I have the following code:如果我有以下代码:

.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 =>

I would like to have the property's value for tsBatchStarted in the file name...我想在文件名中包含tsBatchStarted的属性值...

How do I use an event's property value in the serilog's log file name ?如何serilog 的日志文件名中使用事件的属性值?

dotnet add package Serilog.Sinks.Map

Then:然后:

    .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 =>

Note that this won't cause files to roll, you'll need to use an app based mechanism to clean up logs from old batches.请注意,这不会导致文件滚动,您需要使用基于应用程序的机制来清理旧批次的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM