Skip to content

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.

sh
dotnet add package AlmightyShogun.Serilog

Dependencies ​

Package references ​

  • Microsoft.Extensions.DependencyInjection.Abstractions 10.0.11 — provides the service collection APIs used by the extension methods.
  • Microsoft.Extensions.Hosting 10.0.11 — provides the host builder contract for the IHostBuilder overload.
  • Serilog 4.4.0 — the logging pipeline the package configures.
  • Serilog.AspNetCore 10.0.0 — provides the UseSerilog host builder integration.
  • Serilog.Extensions.Logging 10.0.0 — bridges Serilog to Microsoft.Extensions.Logging.
  • Serilog.Settings.Configuration 10.0.1 — reads the optional Serilog configuration section.
  • Serilog.Sinks.Async 2.1.0 — buffers writes off the logging thread.
  • Serilog.Sinks.Console 6.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.

csharp
using AlmightyShogun.Serilog;
using Microsoft.Extensions.DependencyInjection;

builder.Services.AddCustomLogging(builder.Configuration);
csharp
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.

All packages are released under the MIT License.