C# 滚动文件名中的Serilog自定义上下文,Nlog上下文
类似于,但不是在日志文件(C# 滚动文件名中的Serilog自定义上下文,Nlog上下文,c#,nlog,serilog,C#,Nlog,Serilog,类似于,但不是在日志文件(outputTemplate)中,我想命名该文件 此外,我试图在使用WriteTo.LiterateConsole+WriteTo.RollingFile和WriteTo.NLog之间切换,以维护自定义上下文 但是,为了完成自定义滚动文件命名,我基本上需要在记录器初始化时提供名称,如下所示: var c = new LoggerConfiguration() .Enrich.WithProperty(/* some standard stuff */)
outputTemplate
)中,我想命名该文件
此外,我试图在使用WriteTo.LiterateConsole+WriteTo.RollingFile
和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
;
这样初始化:
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
;
var c=new LoggerConfiguration()
.Enrich.WithProperty(/*一些标准材料*/)
.Enrich.WithProperty(Constants.SourceContextPropertyName,customName)//这是一个只包含链接的答案,所以我只做评论,但我在找到你的问题后发现了这一点:-看起来很有希望。该链接的作者@sander schutten实际上回答了我的问题,但主持人删除了它?
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
;