Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The default console provider from ms works great and the scopes are useful. I want my Serilog RollingFile sink to contain this information too.

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?

like image 659
BillHaggerty Avatar asked Jun 28 '17 13:06

BillHaggerty


People also ask

What is Serilog extensions logging?

Flexible, structured events — log file convenience. NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent . NET platforms. Unlike other logging libraries, Serilog is built with powerful structured event data in mind.

What is the use of Microsoft extensions logging?

Microsoft. Extensions. Logging can also be used for applications that don't use dependency injection, although simple logging can be easier to set up. Microsoft.

Does Serilog use ILogger?

NET 6 as a logging provider. Let's set up Serilog as Logging Provider in the native logging system in . NET so you can use the Microsoft ILogger interface.


1 Answers

You can include the scope by setting the rolling file sink's outputTemplate parameter. The scope property is called {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.

.WriteTo.RollingFile("log-{Date}.txt",
    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {Properties}{NewLine}{Exception}")
like image 116
Nicholas Blumhardt Avatar answered Oct 18 '22 23:10

Nicholas Blumhardt