4

类似于How to output event source Class Name in serilog RollingFile outputTemplate? ,而不是在日志文件 ( outputTemplate) 中,我想命名该文件。

此外,我试图在使用WriteTo.LiterateConsole + WriteTo.RollingFileand之间切换,以WriteTo.NLog维护该自定义上下文。

但是,要完成自定义滚动文件命名,我基本上需要在记录器初始化时提供名称,如下所示:

var c = new LoggerConfiguration()
    .Enrich.WithProperty(/* some standard stuff */)
    .WriteTo.LiterateConsole(minLevel)
    .WriteTo.RollingFile(string.IsNullOrEmpty(customName) ? "logs\log-{Date}.txt" : "logs\"+customName+".log.{Date}.txt") // <-- would have thought I could use `{SourceContext}` as placeholder...
    .CreateLogger()
    .ForContext(Constants.SourceContextPropertyName, customName) // <-- not sure if I need this here
    ;

我想在附加时做类似的事情NLog,所以我可以利用我现有的 NLog 配置行:

archiveFileName="${basedir}/logs/archives/${logger}.log.{#####}.txt"

像这样初始化:

var c = new LoggerConfiguration()
    .Enrich.WithProperty(/* some standard stuff */)
    .Enrich.WithProperty(Constants.SourceContextPropertyName, customName) // <-- is this the trick?
    .WriteTo.Nlog(minLevel)
    .CreateLogger()
    .ForContext(Constants.SourceContextPropertyName, customName) // <-- not sure if this is redundant with enrichment above
    ;
4

0 回答 0