![]() |
VOOZH | about |
dotnet add package Serilog.Settings.Configuration --version 10.0.1
NuGet\Install-Package Serilog.Settings.Configuration -Version 10.0.1
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.1" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="10.0.1" />Directory.Packages.props
<PackageReference Include="Serilog.Settings.Configuration" />Project file
paket add Serilog.Settings.Configuration --version 10.0.1
#r "nuget: Serilog.Settings.Configuration, 10.0.1"
#:package Serilog.Settings.Configuration@10.0.1
#addin nuget:?package=Serilog.Settings.Configuration&version=10.0.1Install as a Cake Addin
#tool nuget:?package=Serilog.Settings.Configuration&version=10.0.1Install as a Cake Tool
A Serilog settings provider that reads from Microsoft.Extensions.Configuration sources, including .NET Core's appsettings.json file.
By default, configuration is read from the Serilog section that should be at the top level of the configuration file.
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "Console" },
{ "Name": "File", "Args": { "path": "Logs/log.txt" } }
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Destructure": [
{ "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
{ "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
{ "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
{ "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
],
"Properties": {
"Application": "Sample"
}
}
}
After installing this package, use ReadFrom.Configuration() and pass an IConfiguration object.
static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
logger.Information("Hello, world!");
}
This example relies on the Microsoft.Extensions.Configuration.Json, Serilog.Sinks.Console, Serilog.Sinks.File, Serilog.Enrichers.Environment and Serilog.Enrichers.Thread packages also being installed.
For a more sophisticated example go to the folder.
Root section name can be changed:
{
"CustomSection": {
...
}
}
var options = new ConfigurationReaderOptions { SectionName = "CustomSection" };
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();
Using section contains list of assemblies in which configuration methods (WriteTo.File(), Enrich.WithThreadId()) reside.
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Enrichers.Thread", /* ... */ ],
// ...
}
For .NET Core projects build tools produce .deps.json files and this package implements a convention using Microsoft.Extensions.DependencyModel to find any package among dependencies with Serilog anywhere in the name and pulls configuration methods from it, so the Using section in example above can be omitted:
{
"Serilog": {
"MinimumLevel": "Debug",
"WriteTo": [ "Console" ],
...
}
}
In order to utilize this convention for .NET Framework projects which are built with .NET Core CLI tools specify PreserveCompilationContext to true in the csproj properties:
<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
In case of non-standard dependency management you can pass a custom DependencyContext object:
var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);
var options = new ConfigurationReaderOptions(functionDependencyContext) { SectionName = "AzureFunctionsJobHost:Serilog" };
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostConfig, options)
.CreateLogger();
Alternatively, you can also pass an array of configuration assemblies:
var configurationAssemblies = new[]
{
typeof(ConsoleLoggerConfigurationExtensions).Assembly,
typeof(FileLoggerConfigurationExtensions).Assembly,
};
var options = new ConfigurationReaderOptions(configurationAssemblies);
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();
For legacy .NET Framework projects it also scans default probing path(s).
For all other cases, as well as in the case of non-conventional configuration assembly names DO use Using section.
Currently, auto-discovery of configuration assemblies is not supported in bundled mode. DO use Using section or explicitly pass a collection of configuration assemblies for workaround.
The MinimumLevel configuration property can be set to a single value as in the sample above, or, levels can be overridden per logging source.
This is useful in ASP.NET Core applications, which will often specify minimum level as:
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
}
MinimumLevel section also respects dynamic reload if the underlying provider supports it.
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "appsettings.json", reloadOnChange: true)
.Build();
Any changes for Default, Microsoft, System sources will be applied at runtime.
(Note: only existing sources are respected for a dynamic update. Inserting new records in Override section is not supported.)
You can also declare LoggingLevelSwitch-es in custom section and reference them for sink parameters:
{
"Serilog": {
"LevelSwitches": { "controlSwitch": "Verbose" },
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341",
"apiKey": "yeEZyL3SMcxEKUijBjN",
"controlLevelSwitch": "$controlSwitch"
}
}
]
}
}
Level updates to switches are also respected for a dynamic update.
Since version 7.0.0, both declared switches (i.e. Serilog:LevelSwitches section) and minimum level override switches (i.e. Serilog:MinimumLevel:Override section) are exposed through a callback on the reader options so that a reference can be kept:
var allSwitches = new Dictionary<string, LoggingLevelSwitch>();
var options = new ConfigurationReaderOptions
{
OnLevelSwitchCreated = (switchName, levelSwitch) => allSwitches[switchName] = levelSwitch
};
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();
LoggingLevelSwitch controlSwitch = allSwitches["$controlSwitch"];
These sections support simplified syntax, for example the following is valid if no arguments are needed by the sinks:
"WriteTo": [ "Console", "DiagnosticTrace" ]
Or alternatively, the long-form ("Name": ...) syntax from the example above can be used when arguments need to be supplied.
By Microsoft.Extensions.Configuration.Json convention, array syntax implicitly defines index for each element in order to make unique paths for configuration keys. So the example above is equivalent to:
"WriteTo": {
"0": "Console",
"1": "DiagnosticTrace"
}
And
"WriteTo:0": "Console",
"WriteTo:1": "DiagnosticTrace"
(The result paths for the keys will be the same, i.e. Serilog:WriteTo:0 and Serilog:WriteTo:1)
When overriding settings with environment variables it becomes less convenient and fragile, so you can specify custom names:
"WriteTo": {
"ConsoleSink": "Console",
"DiagnosticTraceSink": { "Name": "DiagnosticTrace" }
}
This section defines a static list of key-value pairs that will enrich log events.
This section defines filters that will be applied to log events. It is especially useful in combination with Serilog.Expressions (or legacy Serilog.Filters.Expressions) package so you can write expression in text form:
"Filter": [{
"Name": "ByIncludingOnly",
"Args": {
"expression": "Application = 'Sample'"
}
}]
Using this package you can also declare LoggingFilterSwitch-es in custom section and reference them for filter parameters:
{
"Serilog": {
"FilterSwitches": { "filterSwitch": "Application = 'Sample'" },
"Filter": [
{
"Name": "ControlledBy",
"Args": {
"switch": "$filterSwitch"
}
}
]
}
Level updates to switches are also respected for a dynamic update.
Since version 7.0.0, filter switches are exposed through a callback on the reader options so that a reference can be kept:
var filterSwitches = new Dictionary<string, ILoggingFilterSwitch>();
var options = new ConfigurationReaderOptions
{
OnFilterSwitchCreated = (switchName, filterSwitch) => filterSwitches[switchName] = filterSwitch
};
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();
ILoggingFilterSwitch filterSwitch = filterSwitches["filterSwitch"];
Some Serilog packages require a reference to a logger configuration object. The sample program in this project illustrates this with the following entry configuring the Serilog.Sinks.Async package to wrap the Serilog.Sinks.File package. The configure parameter references the File sink configuration:
"WriteTo:Async": {
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "%TEMP%/Logs/serilog-configuration-sample.txt",
"outputTemplate":
"{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
}
}
]
}
},
The same nested pattern applies to wrapping sinks such as Conditional (from Serilog.Expressions), FallbackChain, and Fallible — see the for working examples.
Destructuring means extracting pieces of information from an object and create properties with values; Serilog offers the @ structure-capturing operator. In case there is a need to customize the way log events are serialized (e.g., hide property values or replace them with something else), one can define several destructuring policies, like this:
"Destructure": [
{
"Name": "With",
"Args": {
"policy": "MyFirstNamespace.FirstDestructuringPolicy, MyFirstAssembly"
}
},
{
"Name": "With",
"Args": {
"policy": "MySecondNamespace.SecondDestructuringPolicy, MySecondAssembly"
}
},
{
"Name": "With",
"Args": {
"policy": "MyThirdNamespace.ThirdDestructuringPolicy, MyThirdAssembly"
}
},
],
This is how the first destructuring policy would look:
namespace MyFirstNamespace;
public record MyDto(int Id, int Name);
public class FirstDestructuringPolicy : IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
[NotNullWhen(true)] out LogEventPropertyValue? result)
{
if (value is not MyDto dto)
{
result = null;
return false;
}
result = new StructureValue(new List<LogEventProperty>
{
new LogEventProperty("Identifier", new ScalarValue(deleteTodoItemInfo.Id)),
new LogEventProperty("NormalizedName", new ScalarValue(dto.Name.ToUpperInvariant()))
});
return true;
}
}
Assuming Serilog needs to destructure an argument of type MyDto when handling a log event:
logger.Information("About to process input: {@MyDto} ...", myDto);
it will apply FirstDestructuringPolicy which will convert MyDto instance to a StructureValue instance; a Serilog console sink would write the following entry:
About to process input: {"Identifier": 191, "NormalizedName": "SOME_UPPER_CASE_NAME"} ...
When the configuration specifies a discrete value for a parameter (such as a string literal), the package will attempt to convert that value to the target method's declared CLR type of the parameter. Additional explicit handling is provided for parsing strings to Uri, TimeSpan, enum, arrays and custom collections.
Since version 7.0.0, conversion will use the invariant culture (CultureInfo.InvariantCulture) as long as the ReadFrom.Configuration(IConfiguration configuration, ConfigurationReaderOptions options) method is used. Obsolete methods use the current culture to preserve backward compatibility.
Static member access can be used for passing to the configuration argument via special syntax:
{
"Args": {
"encoding": "System.Text.Encoding::UTF8",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
If the parameter value is not a discrete value, it will try to find a best matching public constructor for the argument:
{
"Name": "Console",
"Args": {
"formatter": {
// `type` (or $type) is optional, must be specified for abstract declared parameter types
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}"
}
}
}
For other cases the package will use the configuration binding system provided by Microsoft.Extensions.Options.ConfigurationExtensions to attempt to populate the parameter. Almost anything that can be bound by IConfiguration.Get<T> should work with this package. An example of this is the optional List<Column> parameter used to configure the .NET Standard version of the Serilog.Sinks.MSSqlServer package.
If parameter type is an interface or an abstract class you need to specify the full type name that implements abstract type. The implementation type should have parameterless constructor.
"Destructure": [
{ "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
...
],
If a Serilog package requires additional external configuration information (for example, access to a ConnectionStrings section, which would be outside of the Serilog section), the sink should include an IConfiguration parameter in the configuration extension method. This package will automatically populate that parameter. It should not be declared in the argument list in the configuration source.
Certain Serilog packages may require configuration information that can't be easily represented by discrete values or direct binding-friendly representations. An example might be lists of values to remove from a collection of default values. In this case the method can accept an entire IConfigurationSection as a call parameter and this package will recognize that and populate the parameter. In this way, Serilog packages can support arbitrarily complex configuration scenarios.
hosts.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext" ],
"WriteTo": [
{ "Name": "Seq", "Args": { "serverUrl": "http://localhost:5341" } }
]
}
}
In Startup.cs section name should be prefixed with AzureFunctionsJobHost
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ILoggerProvider>(sp =>
{
var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);
var hostConfig = sp.GetRequiredService<IConfiguration>();
var options = new ConfigurationReaderOptions(functionDependencyContext) { SectionName = "AzureFunctionsJobHost:Serilog" };
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostConfig, options)
.CreateLogger();
return new SerilogLoggerProvider(logger, dispose: true);
});
}
}
In order to make auto-discovery of configuration assemblies work, modify Function's csproj file
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="FunctionsPostBuildDepsCopy" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(OutDir)bin\$(AssemblyName).deps.json" />
</Target>
<Target Name="FunctionsPublishDepsCopy" AfterTargets="Publish">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(PublishDir)bin\$(AssemblyName).deps.json" />
</Target>
</Project>
This package tracks the versioning and target framework support of its Microsoft.Extensions.Configuration dependency.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Serilog.Settings.Configuration:
| Package | Downloads |
|---|---|
|
Serilog.AspNetCore
Serilog support for ASP.NET Core logging |
|
|
Umbraco.Cms.Web.Common
Contains the web assembly needed to run Umbraco CMS. |
|
|
Umbraco.Cms.Infrastructure
Contains the infrastructure assembly needed to run Umbraco CMS. |
|
|
Umbraco.Cms.Examine.Lucene
Adds Examine searching support using Lucene to Umbraco CMS. |
|
|
Umbraco.Cms.Web.Website
Contains the website assembly needed to run the frontend of Umbraco CMS. |
Showing the top 20 popular GitHub repositories that depend on Serilog.Settings.Configuration:
| Repository | Stars |
|---|---|
|
jellyfin/jellyfin
The Free Software Media System - Server Backend & API
|
|
|
Kareadita/Kavita
Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
|
|
|
RayWangQvQ/BiliBiliToolPro
B 站(bilibili)自动任务工具,支持docker、青龙、k8s等多种部署方式。全面拥抱AI。敏感肌也能用。
|
|
|
microsoft/ailab
Experience, Learn and Code the latest breakthrough innovations with Microsoft AI
|
|
|
kurrent-io/KurrentDB
KurrentDB is a database that's engineered for modern software applications and event-driven architectures. Its event-native design simplifies data modeling and preserves data integrity while the integrated streaming engine solves distributed messaging challenges and ensures data consistency.
|
|
|
win-acme/win-acme
Automate SSL/TLS certificates on Windows with ease
|
|
|
anjoy8/Blog.Core
💖 ASP.NET Core 8.0 全家桶教程,前后端分离后端接口,vue教程姊妹篇,官方文档:
|
|
|
umbraco/Umbraco-CMS
Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
|
|
|
dotnetcore/Util
Util是一个.Net平台下的应用框架,旨在提升中小团队的开发能力,由工具类、分层架构基类、Ui组件,配套代码生成模板,权限等组成。
|
|
|
simplcommerce/SimplCommerce
A simple, cross platform, modulith ecommerce system built on .NET
|
|
|
Ombi-app/Ombi
Want a Movie or TV Show on Plex/Emby/Jellyfin? Use Ombi!
|
|
|
rnwood/smtp4dev
smtp4dev - the fake smtp email server for development and testing
|
|
|
skoruba/IdentityServer4.Admin
The administration for the IdentityServer4 and Asp.Net Core Identity
|
|
|
fullstackhero/blazor-starter-kit
Clean Architecture Template for Blazor WebAssembly Built with MudBlazor Components.
|
|
|
dafny-lang/dafny
Dafny is a verification-aware programming language
|
|
|
microsurging/surging
Surging is a micro-service engine that provides a lightweight, high-performance, modular RPC request pipeline. support Event-based Asynchronous Pattern and reactive programming.
|
|
|
ErsatzTV/legacy
Open-source platform that transforms your personal media library into live, custom TV channels.
|
|
|
exceptionless/Exceptionless
Exceptionless application
|
|
|
Impostor/Impostor
Impostor - An open source reimplementation of the Among Us Server
|
|
|
Cleanuparr/Cleanuparr
Advanced download manager for the Servarr ecosystem
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.1 | 54,955 | 6/15/2026 |
| 10.0.1-dev-02351 | 1,668 | 5/26/2026 |
| 10.0.1-dev-02350 | 137 | 5/26/2026 |
| 10.0.1-dev-02346 | 4,061 | 5/2/2026 |
| 10.0.1-dev-02345 | 152 | 5/2/2026 |
| 10.0.1-dev-02343 | 208 | 4/30/2026 |
| 10.0.1-dev-02342 | 151 | 4/30/2026 |
| 10.0.1-dev-02339 | 5,113 | 4/9/2026 |
| 10.0.1-dev-02334 | 543 | 4/7/2026 |
| 10.0.1-dev-02330 | 38,675 | 1/21/2026 |
| 10.0.0 | 33,567,895 | 11/26/2025 |
| 10.0.0-dev-02326 | 810 | 11/26/2025 |
| 10.0.0-dev-02325 | 311 | 11/26/2025 |
| 10.0.0-dev-02323 | 319 | 11/26/2025 |
| 10.0.0-dev-02322 | 330 | 11/26/2025 |
| 9.0.1-dev-02319 | 30,629 | 10/30/2025 |
| 9.0.1-dev-02317 | 269,468 | 1/16/2025 |
| 9.0.0 | 91,669,234 | 12/9/2024 |
| 9.0.0-dev-02314 | 1,245 | 12/6/2024 |
| 9.0.0-dev-02313 | 2,297 | 12/3/2024 |