简体   繁体   English

dotnetcore控制台上的Microsoft.Extensions.Logging和Serilog。在RollingFile接收器中包含范围信息

[英]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.

相关问题 Serilog 模板和 Microsoft.Extensions.Logging - Serilog templates and Microsoft.Extensions.Logging Serilog、Microsoft.Extensions.Logging 和 Autofac - Serilog, Microsoft.Extensions.Logging and Autofac 如何在多线程的Microsoft.Extensions.Logging中包含DependencyInjection范围? - How to include the DependencyInjection scope in Microsoft.Extensions.Logging with multithreading? 如何获取 Microsoft.Extensions.Logging<T> 在使用 Serilog 和 AutoFac 的控制台应用程序中? - How to get Microsoft.Extensions.Logging<T> in console application using Serilog and AutoFac? 如何在控制台中格式化日志输出?(Microsoft.Extensions.Logging) - How to format the output of logs in the console?(Microsoft.Extensions.Logging) 使用 Microsoft 的 ILogger 时,如何将 scope 包含在 Serilog 的控制台接收器中? - How do I include scope with Serilog's console sink when using Microsoft's ILogger? Microsoft.Extensions.Logging - LogError 未记录异常 - Microsoft.Extensions.Logging - LogError is not logging exception Net Core 3.0 和 Serilog 产生 output 像 Microsoft.Extensions.Logging - Net Core 3.0 and Serilog producing output like Microsoft.Extensions.Logging 如何使用LoggerFactory和Microsoft.Extensions.Logging进行.NET Core Console使用C#进行日志记录 - How to Use LoggerFactory and Microsoft.Extensions.Logging for .NET Core Console Logging With C# 如何使用 Microsoft.Extensions.Logging 配置和查看日志记录 - How to configure and view logging using Microsoft.Extensions.Logging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM