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.
{
"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")
.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 sample folder.
Root section name can be changed:
{
"CustomSection": {
...
}
}
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, sectionName: "CustomSection")
.CreateLogger();
Using
section contains list of assemblies in wich configuration methods (WriteTo.File()
, Enrich.WithThreadId()
) resides.
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 ommitted:
{
"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 logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostConfig, sectionName: "AzureFunctionsJobHost:Serilog", dependencyContext: functionDependencyContext)
.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.
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 respect 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.
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.Configuratiom.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 usefull in combination with Serilog.Filters.Expression package so you can write expression in text form:
"Filter": [{
"Name": "ByIncludingOnly",
"Args": {
"expression": "Application = 'Sample'"
}
}]
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}"
}
}
]
}
},
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.
If the parameter value is not a discrete value, 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 logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostConfig, sectionName: "AzureFunctionsJobHost:Serilog", dependencyContext: functionDependencyContext)
.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">
<!-- ... -->
<!-- add this targets -->
<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="$(PublishDir)$(AssemblyName).deps.json" DestinationFiles="$(PublishDir)bin\$(AssemblyName).deps.json" />
</Target>
</Project>