skip to content

With Serilog.Settings.Configuration, how is the "Serilog" section of appsettings.json read, and which parts of it change at runtime when the file is edited?

level: middleimportance: should knowfreq 55%

answer

  1. ReadFrom.Configuration(IConfiguration)
  2. Using, MinimumLevel, WriteTo with Name and Args
  3. packages with Serilog in the name auto-discovered
  4. single-file: list Using or pass assemblies
  5. reload: levels yes, new overrides and sinks no

basics

~20 s

ReadFrom.Configuration(configuration) maps the Serilog section onto LoggerConfiguration: WriteTo entries become sink method calls with Args as named parameters, MinimumLevel becomes levels and overrides. On reload, only existing level values change; new Override keys and sink changes need a restart.

solid answer

~40 s

`ReadFrom.Configuration(builder.Configuration)` from `Serilog.Settings.Configuration` reads the top-level `Serilog` section (renameable through `ConfigurationReaderOptions.SectionName`). Each `WriteTo` entry names an extension method, such as `"File"`, and its `Args` bind to that method's parameters by name. `Using` lists assemblies to search, but it can be omitted because the reader scans the app's dependencies for packages with Serilog in the name. That scan does not work in a single-file app, which throws unless `Using` is present or assemblies are passed in. `MinimumLevel` is a level or an object with `Default` and `Override`. Each level becomes a `LoggingLevelSwitch`, so with `reloadOnChange` an edited level takes effect live; new `Override` keys and sink settings do not.

go deeper

for a junior

Recognise the Serilog section: MinimumLevel, WriteTo with Name and Args, and that ReadFrom.Configuration turns it into the same calls you would write in code.

for a middle

Explain Args binding by parameter name, the Override map, Using versus auto-discovery, and exactly what reloads: existing level values only.

for a senior

Anticipate the single-file InvalidOperationException, silent skips of misnamed sinks visible only in SelfLog, and the need to pre-declare overrides you may tune live.

for a principal

Decide which logging settings operations may change per environment through configuration and which stay in code, so a bad config cannot silently drop production logging.

## From JSON to builder calls `Serilog.Settings.Configuration` adds `ReadFrom.Configuration(...)` to `LoggerConfiguration`. It takes any `IConfiguration` - in ASP.NET Core usually `builder.Configuration`, which already merges `appsettings.json`, `appsettings.{Environment}.json`, environment variables and command-line arguments - and replays the `Serilog` section as builder calls. The preferred overload is `ReadFrom.Configuration(configuration, readerOptions)`; the older overloads that took a section name or assemblies directly are marked `[Obsolete]` and point to `ConfigurationReaderOptions`. A call-centre configuration might look like this: ```json { "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { "Default": "Information", "Override": { "Microsoft.AspNetCore": "Warning", "CallCentre.Telephony": "Debug" } }, "WriteTo": [ { "Name": "Console" }, { "Name": "File", "Args": { "path": "logs/callcentre-.txt", "rollingInterval": "Day", "retainedFileCountLimit": 14 } } ] } } ``` ## The sections and what they map to | Key | Becomes | |---|---| | `Using` | assemblies searched for configuration methods | | `MinimumLevel` (string) | `MinimumLevel.Is(level)` | | `MinimumLevel.Default` / `Override` | default level plus `MinimumLevel.Override(source, ...)` | | `LevelSwitches` | named `LoggingLevelSwitch` objects (`"$name"`) sinks can reference | | `WriteTo` / `AuditTo` | sink extension methods, `Args` bound by parameter name | | `Enrich`, `Destructure`, `Filter`, `Properties` | the matching builder sections | Arguments are converted with the invariant culture when the options overload is used, enum values such as `"Day"` bind to `RollingInterval.Day`, and an argument you omit keeps the method's default. Because JSON arrays become keys `WriteTo:0`, `WriteTo:1`, overriding a sink from an environment variable means addressing it by index, which is why the reader also accepts named children (`"WriteTo": { "FileSink": { "Name": "File" } }`). ## Finding the sink assemblies The reader needs the assemblies that contain `WriteTo.File`, `WriteTo.Console` and so on: 1. Assemblies listed in `Using` are loaded. 2. Then it uses the app's `.deps.json` (through `Microsoft.Extensions.DependencyModel`) to load every dependency with **Serilog** in its name - so `Using` is usually optional. 3. A **single-file** app (`PublishSingleFile`) has no usable dependency context for this scan. If the section needs anything beyond `MinimumLevel`, `LevelSwitches` and `Properties`, the reader throws `InvalidOperationException` explaining that the app is probably single-file. The fix is a `Using` list or `new ConfigurationReaderOptions(typeof(ConsoleLoggerConfigurationExtensions).Assembly, typeof(FileLoggerConfigurationExtensions).Assembly)`. A sink whose name matches no method (a typo, or a package that is not referenced) does not throw: the reader writes "Unable to find a method called ..." to `SelfLog` and skips it. ## What reloads and what does not Every level the reader creates - `Default`, each `Override` entry and each `LevelSwitches` entry - is a `LoggingLevelSwitch` subscribed to the configuration's reload token. When the provider reloads (the host's `appsettings.json` source does by default): - changing `"CallCentre.Telephony": "Debug"` to `"Information"` takes effect without a restart; - **adding a new key** under `Override` is not picked up - the set of overrides was fixed when the logger was built; - sink settings (`WriteTo` paths, templates, retention) are not re-applied; the sinks were constructed once; - an invalid level at startup, such as `"Trace"` (a Microsoft.Extensions.Logging name, not a Serilog one), throws `InvalidOperationException`; the same mistake in a reload is only written to `SelfLog` and the old level stays. ## Mixing configuration and code `ReadFrom.Configuration(...)` is just another builder call, so it composes with code. Calls are applied in order: configuration can supply the levels and the file sink, and code can add a sink that needs a live object configuration cannot express: ```csharp Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .WriteTo.Console() .CreateLogger(); ``` Two cautions follow. First, a sink listed in both places is added twice, and every event is written twice. Second, a level set in code after `ReadFrom.Configuration` replaces the configured default, so the file no longer controls it. For values operations should own, keep them in configuration only. Named switches from the `LevelSwitches` section (`"LevelSwitches": { "$telephony": "Information" }`) can be referenced from `MinimumLevel` and from sink `Args` such as `levelSwitch`, so one entry in the file moves several thresholds at once. ## Practical guidance - Put the shared shape in `appsettings.json` and per-environment levels in `appsettings.Production.json`. - Pre-declare every override you might want to tune live, even at its current level. - Keep `Using` in the file if the app might ever be published as single-file.

  • How do you override the default level for one environment without editing appsettings.json?
    Use any configuration source that maps onto the same key. An environment variable `Serilog__MinimumLevel__Default=Warning` or an `appsettings.Production.json` with the same `Serilog:MinimumLevel:Default` path wins over the base file, because `ReadFrom.Configuration` reads the merged `IConfiguration`, not the JSON file.
  • Why might a sink configured in appsettings.json silently produce nothing?
    If its `Name` matches no extension method in the loaded assemblies - a typo, or the sink package not referenced - the reader skips it and writes "Unable to find a method called ..." to `SelfLog`. Enabling `SelfLog.Enable(Console.Error)` before `ReadFrom.Configuration` runs makes that message visible.
  • How can code get hold of the level switches the reader creates?
    Pass `new ConfigurationReaderOptions { OnLevelSwitchCreated = (name, sw) => switches[name] = sw }`. The callback receives each declared switch and each override switch, so an admin endpoint can change them directly instead of editing the file.

  • Adding a new Override key at runtime is picked up when reloadOnChange is on
  • Editing a WriteTo path in appsettings.json reopens the file sink live
  • Using must always list every sink package or nothing is loaded
  • "Trace" is a valid Serilog level name in the MinimumLevel section
  • ReadFrom.Configuration reads only appsettings.json, not environment variables

Assumes Serilog.Settings.Configuration 10.0.1 with Serilog 4.4; the ConfigurationReaderOptions overload and the OnLevelSwitchCreated callback date from its 7.0 release.