Installation
Install AlmightyShogun.Serilog in the application that owns logging setup. The package targets net10.0 and brings Serilog with it, so the application does not need to reference Serilog directly.
dotnet add package AlmightyShogun.SerilogDependencies
Package references
Microsoft.Extensions.DependencyInjection.Abstractions10.0.11— provides the service collection APIs used by the extension methods.Microsoft.Extensions.Hosting10.0.11— provides the host builder contract for theIHostBuilderoverload.Serilog4.4.0— the logging pipeline the package configures.Serilog.AspNetCore10.0.0— provides theUseSeriloghost builder integration.Serilog.Extensions.Logging10.0.0— bridges Serilog toMicrosoft.Extensions.Logging.Serilog.Settings.Configuration10.0.1— reads the optionalSerilogconfiguration section.Serilog.Sinks.Async2.1.0— buffers writes off the logging thread.Serilog.Sinks.Console6.1.1— writes formatted output to the console.
Startup Registration
Call AddCustomLogging once during startup. The IHostBuilder receiver hands the host's logging to Serilog outright, while the IServiceCollection receiver adds it as one more provider, for startup code that never sees the host builder.
using AlmightyShogun.Serilog;
using Microsoft.Extensions.DependencyInjection;
builder.Services.AddCustomLogging(builder.Configuration);using AlmightyShogun.Serilog;
using Microsoft.Extensions.Hosting;
IHost host = Host.CreateDefaultBuilder(args)
.AddCustomLogging()
.Build();Flushing on shutdown
The console sink is asynchronous, so log events are buffered and written on a background thread. The package registers the logger for disposal, which flushes that buffer, but disposal only happens during an orderly shutdown.
An application that exits by killing the process, or that suppresses shutdown signals, can still lose the last buffered events. If the host also uses AlmightyShogun.Hosting.ConsoleLifetime, UseCustomConsoleLifetime makes SIGTERM shut down cleanly, which is what allows the flush to run.