滚动文件名,Nlog上下文中的Serilog自定义上下文

时间:2015-07-06 21:07:48

标签: c# nlog serilog

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

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

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

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
    ;

0 个答案:

没有答案