In an ASP.NET Core 10 app, what does builder.Services.AddSerilog() change about ILogger<T> logging, and what happens to the Logging section and built-in providers?
answer
- package Serilog.AspNetCore
- replaces the ILoggerFactory singleton
- category becomes SourceContext
- Logging:LogLevel no longer applies
- writeToProviders: true to keep providers
basics
~20 sAddSerilog() registers Serilog's ILoggerFactory, so every ILogger<T> writes through the Serilog pipeline, with the category as SourceContext. Levels then come from Serilog's MinimumLevel and overrides; the Logging section and the built-in console and debug providers are bypassed.
solid answer
~40 s`Serilog.AspNetCore` (whose dependencies bring in `Serilog.Extensions.Hosting`, the console, file and debug sinks, compact JSON formatting and `Serilog.Settings.Configuration`) adds `AddSerilog` to `IServiceCollection`. It registers a singleton `ILoggerFactory` implemented by `SerilogLoggerFactory`, which replaces the default factory, so every `ILogger<T>` from ASP.NET Core, EF Core or your own code becomes a Serilog event whose `SourceContext` is the category name. The parameterless `AddSerilog()` uses the static `Log.Logger`; the overloads taking a configuration callback build the logger inside the container. Because the default factory is gone, the `Logging:LogLevel` section no longer filters anything - use Serilog's `MinimumLevel` and `Override` instead - and the built-in console, debug and event-source providers receive nothing unless you pass `writeToProviders: true`. Every overload registers `IDiagnosticContext` as a singleton; the overloads that receive or build a logger also register Serilog's own `ILogger`.
go deeper
Know the package and the one line: builder.Services.AddSerilog(), after which injected ILogger<T> writes to Serilog's sinks.
Explain that AddSerilog replaces the ILoggerFactory, why the Logging section stops working, and that the category becomes SourceContext for overrides.
Choose between the overloads by who owns and disposes the logger, and know when writeToProviders or preserveStaticLogger is needed.
Decide whether one logging pipeline replaces Microsoft.Extensions.Logging providers across services, and how that affects vendor integrations and configuration conventions.
## Two logging systems, one pipeline ASP.NET Core logs through **Microsoft.Extensions.Logging** (MEL): code asks for `ILogger<T>`, a logger factory creates it, and registered providers (console, debug, event source, and event log on Windows, all added by the default host) write the output, filtered by the `Logging` configuration section. **Serilog** is a separate library with its own pipeline. `Serilog.AspNetCore` connects them so that framework events and your own events land in the same Serilog sinks, with the same levels and enrichers. ```bash dotnet add package Serilog.AspNetCore ``` The package depends on `Serilog`, `Serilog.Extensions.Hosting`, `Serilog.Settings.Configuration`, `Serilog.Formatting.Compact` and the console, file and debug sinks, so a typical app needs nothing else. Its version tracks `Microsoft.Extensions.Hosting`: a .NET 10 app uses Serilog.AspNetCore 10.x. ## What AddSerilog registers `builder.Services.AddSerilog(...)` comes from `Serilog.Extensions.Hosting`. Depending on the overload it registers: - a singleton **`ILoggerFactory`** implemented by `SerilogLoggerFactory` - the key step; - the Serilog `ILogger` itself, for code that wants Serilog's API directly - registered by the overloads that receive or build a logger, not by the parameterless one; - **`IDiagnosticContext`** (and the concrete `DiagnosticContext`) as singletons, used by request logging. The default host registered MEL's `LoggerFactory` with a try-add; the later registration is the one the container resolves, so `ILogger<T>` (which is created through `ILoggerFactory`) now wraps Serilog. Each category name, such as `Microsoft.AspNetCore.Hosting.Diagnostics` or `HotelBooking.Api.BookingsController`, becomes the event's `SourceContext`, which is what Serilog's `MinimumLevel.Override` matches. ## The overloads | Call | Logger used | Who disposes it | |---|---|---| | `AddSerilog()` | the static `Log.Logger`, configured before the host | you, with `Log.CloseAndFlush()` | | `AddSerilog(logger, dispose: true)` | the instance you pass | the factory, on host shutdown | | `AddSerilog(lc => ...)` or `AddSerilog((services, lc) => ...)` | built inside the container; also assigned to `Log.Logger` unless `preserveStaticLogger: true` | the factory, through `Log.CloseAndFlush()`, unless it froze a bootstrap logger | `builder.Host.UseSerilog(...)` on `IHostBuilder` still exists and simply calls `AddSerilog`; the package README now shows `builder.Services.AddSerilog()`. ## What stops working Because MEL's own `LoggerFactory` is no longer used: 1. **The `Logging` section is ignored.** `"Logging": { "LogLevel": { "Default": "Warning" } }` no longer filters anything; the README tells you to remove it. Express the same intent as `"Serilog": { "MinimumLevel": { "Default": "Information", "Override": { "Microsoft.AspNetCore": "Warning" } } }`. 2. **Built-in providers go quiet.** `AddConsole()`, `AddDebug()` and the rest are still registered but receive no events. Pass `writeToProviders: true` if some MEL provider must keep receiving events; Serilog then forwards to the registered providers. 3. **`builder.Logging.SetMinimumLevel(...)` has no effect** on what Serilog writes, for the same reason. What keeps working: `ILogger<T>` injection, message templates with named holes, `BeginScope` properties (carried into the event when the Serilog configuration has `Enrich.FromLogContext()`), and every library that logs through MEL. ## Checking the wiring Two quick checks confirm that framework events now flow through Serilog: - Add `{SourceContext}` to the console sink's output template for a while, as the Serilog.AspNetCore README suggests; lines from Kestrel, routing and your controllers then show their category names, which are also the names to use in `Override`. - Change a Serilog override, for example `Microsoft.AspNetCore` to `Warning`, and watch the framework's Information lines disappear; changing `Logging:LogLevel` should visibly do nothing. Application code can keep injecting `ILogger<T>`, which is the usual choice because libraries and framework code use it too. Code that wants Serilog-specific features, such as `ForContext` with `destructureObjects`, can use the static `Log` class or the Serilog `ILogger` the callback overloads register. ## A hotel-booking API set up minimally ```csharp using Serilog; Log.Logger = new LoggerConfiguration() .MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console() .CreateLogger(); try { var builder = WebApplication.CreateBuilder(args); builder.Services.AddSerilog(); var app = builder.Build(); app.MapGet("/api/hotels/{id}", (int id, ILogger<Program> log) => { log.LogInformation("Hotel {HotelId} viewed", id); return Results.Ok(); }); app.Run(); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); } finally { Log.CloseAndFlush(); } ``` The `LogInformation` call reaches the Serilog console sink with `SourceContext` = `Program` and a `HotelId` property.
- After switching to Serilog, the team still edits Logging:LogLevel to quiet EF Core and nothing changes. Why, and what is the fix?The `Logging` section is read by MEL's `LoggerFactory`, which `AddSerilog` replaced with `SerilogLoggerFactory`. Serilog decides levels with its own `MinimumLevel`, so add `"Microsoft.EntityFrameworkCore": "Warning"` under `Serilog:MinimumLevel:Override` and delete the dead `Logging` section to avoid confusion.
- When would you pass writeToProviders: true?When some Microsoft.Extensions.Logging provider must keep receiving events alongside Serilog's sinks - for example a vendor provider that has no Serilog sink. Serilog then forwards events to the registered `ILoggerProvider`s. Without it, those providers stay registered but silent.
- The Logging:LogLevel section still filters events after AddSerilog
- AddSerilog adds Serilog as one more provider next to the console provider
- Only Serilog's static Log class can write to Serilog sinks; ILogger<T> cannot
- builder.Logging.SetMinimumLevel controls what Serilog writes
- Serilog.AspNetCore requires separately installing the console and file sinks
Assumes Serilog.AspNetCore 10.0 (tracking Microsoft.Extensions.Hosting 10) on Serilog 4.4 and ASP.NET Core 10; AddSerilog on IServiceCollection lives in Serilog.Extensions.Hosting.