skip to content

In Serilog, how do MinimumLevel.Override, a sink's restrictedToMinimumLevel and a LoggingLevelSwitch decide whether an event from a given SourceContext is written?

level: middleimportance: should knowfreq 50%

answer

  1. override keyed by SourceContext prefix
  2. most specific namespace wins
  3. override replaces the default, can be lower
  4. sink restriction only filters further
  5. MinimumLevel.ControlledBy(levelSwitch)

basics

~20 s

MinimumLevel.Override sets the effective minimum for events whose SourceContext starts with a namespace, replacing the default level. A sink's restrictedToMinimumLevel filters further but cannot let through events the logger dropped. A LoggingLevelSwitch makes either level changeable at runtime.

solid answer

~30 s

Every event first meets the logger's **effective minimum**. Without overrides that is the `MinimumLevel` default. With `MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)`, a logger whose `SourceContext` (set by `ForContext<T>()` or an `ILogger<T>` category) is that namespace or below it uses `Warning` instead. The most specific match wins, and an override can be **more verbose** than the default too. Only events that pass that check reach the sinks, where `restrictedToMinimumLevel` can filter again but never loosen. To change levels without restarting, pass a `LoggingLevelSwitch` to `MinimumLevel.ControlledBy(...)`, to `Override(source, levelSwitch)` or to a sink's `levelSwitch` parameter, and set its `MinimumLevel` property later.

go deeper

for a junior

Know that MinimumLevel.Override quiets noisy namespaces such as Microsoft.AspNetCore, and that a sink can be restricted to Warning and above.

for a middle

Explain the two-stage check: effective minimum by SourceContext prefix, most specific first and replacing the default, then per-sink restriction that can only narrow.

for a senior

Use LoggingLevelSwitch to change one namespace's level at runtime, and know overrides depend on SourceContext, so static Log calls ignore them.

for a principal

Decide who may flip a level switch in production and how it is reverted, since a Debug override on a busy namespace changes volume and cost immediately.

## Three places a level can be set Serilog decides whether to write an event in two stages, and three settings feed them: - **The logger's default minimum** - `MinimumLevel.Information()`, `MinimumLevel.Is(...)` or, for a runtime-changeable value, `MinimumLevel.ControlledBy(levelSwitch)`. Without any call it is `Information`. - **Per-source overrides** - `MinimumLevel.Override(source, level)` or `Override(source, levelSwitch)`. - **Per-sink restrictions** - the `restrictedToMinimumLevel` (or `levelSwitch`) argument that most `WriteTo` methods accept. The first two decide whether the event is created at all; the third decides which sinks receive it. ## How an override is matched An override is keyed by a **source**: a namespace or full type name. Serilog compares it with the event's `SourceContext` property, which `ForContext<T>()` sets to the type's full name and which the Microsoft.Extensions.Logging bridge sets to the `ILogger<T>` category. The rules, as implemented in `LevelOverrideMap`: 1. The source must be a prefix of the context and end on a dot boundary, so `"Microsoft"` matches `Microsoft.AspNetCore.Hosting.Diagnostics` but not `MicrosoftTeams.Bot`. 2. The comparison ignores case. 3. Overrides are checked from the most specific downwards, so `"Microsoft.Hosting.Lifetime"` beats `"Microsoft"` for the lifetime logger. 4. A matching override **replaces** the default for that logger - it is not combined with it. 5. Events with no `SourceContext` (plain `Log.Information(...)`) always use the default. Rule 4 has a consequence people miss: the override can be lower than the default. In a call-centre app you can keep the default at `Information` and still see `Debug` from the telephony code: ```csharp var telephonySwitch = new LoggingLevelSwitch(LogEventLevel.Information); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .MinimumLevel.Override("CallCentre.Telephony", telephonySwitch) .WriteTo.Console() .WriteTo.File("logs/errors-.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Warning) .CreateLogger(); // later, from an admin endpoint or a debugger session: telephonySwitch.MinimumLevel = LogEventLevel.Debug; ``` ## Sink restrictions only narrow After the level check, the event goes to every sink. A sink created with `restrictedToMinimumLevel: LogEventLevel.Warning` ignores anything below Warning. The reverse does not work: `restrictedToMinimumLevel: LogEventLevel.Debug` on a sink cannot bring back a `Debug` event that the logger's minimum already dropped, because that event was never created. If one sink should see `Debug`, lower the logger (or the override) and restrict the *other* sinks. | Setting | Scope | Can make output more verbose? | |---|---|---| | `MinimumLevel.X()` / `ControlledBy` | whole logger, default | yes | | `MinimumLevel.Override(source, ...)` | loggers whose SourceContext matches | yes, for that source | | `restrictedToMinimumLevel` on a sink | one sink | no, only stricter | ## LoggingLevelSwitch `LoggingLevelSwitch` is a small mutable object holding a `MinimumLevel` (its constructor defaults to `Information`) and raising a `MinimumLevelChanged` event. Anything configured with a switch reads its current value on each event, so assigning `levelSwitch.MinimumLevel = LogEventLevel.Debug` takes effect immediately, with no logger rebuild. One switch can drive several places at once - the default level and a sink, for example. Serilog.Settings.Configuration builds switches for you from the `MinimumLevel` section and can expose them through `ConfigurationReaderOptions.OnLevelSwitchCreated`. ## Finding the name to override An override only helps if you know the exact `SourceContext` of the noisy logger. Two practical ways to find it: - Add `{SourceContext}` to the console sink's `outputTemplate` for a while; every line then shows the logger name, which for ASP.NET Core and EF Core is the full type name of the component writing it. - Look at one event in a structured sink (JSON file or log server), where `SourceContext` is an ordinary property. Then choose the shortest prefix that covers the noise without hiding something you need. `"Microsoft.EntityFrameworkCore.Database.Command"` silences SQL command logging alone, while `"Microsoft.EntityFrameworkCore"` also hides migration and model warnings. Because the most specific entry wins, a broad `Warning` override plus one narrower `Information` override for the few categories you still want is a common, readable pattern. ## Limits and traps - Overrides are not supported on sub-loggers created with `WriteTo.Logger(...)`; use a filter or `WriteTo.Conditional(...)` there. - An override lists a namespace, not a sink: there is no per-sink override map. - Overrides only see `SourceContext`. A static `Log.Information` call in `CallCentre.Telephony` code has no context unless it goes through `ForContext<T>()` or an `ILogger<T>`, so the override does not apply to it.

  • Why does an override for "Microsoft" not apply to a static Log.Warning call made inside a Microsoft-named namespace of your own code?
    Overrides are matched against the event's `SourceContext` property, not the calling code's namespace. A static `Log.Warning(...)` call carries no `SourceContext`, so it falls back to the default level. Loggers created through `ForContext<T>()`, `ForContext(Type)` or an `ILogger<T>` category set the `SourceContext` that overrides match against.
  • You configured Override("Microsoft", Warning) and Override("Microsoft.Hosting.Lifetime", Information). Which applies to Microsoft.Hosting.Lifetime?
    `Information`. Serilog checks overrides from the most specific source down and stops at the first one that matches on a dot boundary, so the longer `Microsoft.Hosting.Lifetime` entry wins and the startup and shutdown messages still appear while the rest of `Microsoft.*` is held at Warning.
  • How do you raise one namespace to Debug in production without a restart?
    Configure that namespace's override with a `LoggingLevelSwitch` instead of a fixed level, keep a reference to it, and set its `MinimumLevel` to `Debug` when needed - for example from an authorized admin endpoint. The switch is read on every event, so the change is immediate and can be reverted the same way.

  • An override can only make a namespace quieter, never more verbose than the default
  • restrictedToMinimumLevel Debug on a sink lets Debug events through an Information logger
  • Overrides match the namespace of the code that calls Log.Information
  • The first override declared wins, whatever its specificity
  • Changing a LoggingLevelSwitch requires rebuilding the logger with CreateLogger