[英]Microsoft.Extensions.Logging and Serilog on dotnetcore console. Include scope info in RollingFile sink
I am using the Microsoft.Extensions.Logging.ILogger abstraction which provides the BeginScope function, which prepends hierarchical information onto any log between the creation and disposal of the scope. 我正在使用Microsoft.Extensions.Logging.ILogger抽象,它提供BeginScope函数,该函数将分层信息预先添加到创建和处理作用域之间的任何日志中。
The default console provider from ms works great and the scopes are useful. ms的默认控制台提供程序运行良好,范围很有用。 I want my Serilog RollingFile sink to contain this information too.
我希望我的Serilog RollingFile接收器也包含这些信息。
Config code: 配置代码:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Debug)
.Enrich.WithThreadId()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.WriteTo.RollingFile(pathFormat: "/opt/iqdata/logs/log-{Date}.txt", restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information, retainedFileCountLimit:30, flushToDiskInterval:TimeSpan.FromSeconds(15), shared:true)
.Enrich.WithProperty("process", "Worker")
.CreateLogger();
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger).AddConsole(includeScopes: true, minLevel: LogLevel.Debug);
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "AppSettings.json", optional: false, reloadOnChange: true).Build();
provider = new ServiceCollection()
.AddOptions()
.AddSingleton(loggerFactory)
.BuildServiceProvider();
logger = provider.GetService<ILoggerFactory>().CreateLogger("Worker.Program");
logger.LogDebug("Current directory:{CurrentDirectory}", Directory.GetCurrentDirectory());
So what do I need to do for scope info to be written to the file? 那么我需要做什么才能将范围信息写入文件? Also as a bonus what do I do to Include the Enriched values like ProcessName in the RollingFile sink?
另外作为奖励我该如何在RollingFile接收器中包含像ProcessName这样的丰富值?
You can include the scope by setting the rolling file sink's outputTemplate
parameter. 您可以通过设置滚动文件接收器的
outputTemplate
参数来包括作用域。 The scope property is called {Scope}
. scope属性称为
{Scope}
。
Using Serilog 2.5, however, you can include both the scope and any additional enriched properties using {Properties}
in the output template, as shown below. 但是,使用Serilog 2.5,您可以使用输出模板中的
{Properties}
包括范围和任何其他丰富属性,如下所示。
.WriteTo.RollingFile("log-{Date}.txt",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {Properties}{NewLine}{Exception}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.