- Notifications
You must be signed in to change notification settings - Fork26
An <appSettings> configuration reader for Serilog
License
serilog/serilog-settings-appsettings
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
An XML<appSettings> reader forSerilog.
The<appSettings> support package needs to be installed from NuGet:
Install-Package Serilog.Settings.AppSettingsTo read configuration from<appSettings> use theReadFrom.AppSettings() extension method on yourLoggerConfiguration:
Log.Logger=newLoggerConfiguration().ReadFrom.AppSettings() ...// Other configuration here, then.CreateLogger()
You can mix and match XML and code-based configuration, but each sink must be configuredeither using XMLor in code - sinks added in code can't be modified via app settings.
To configure the logger, an<appSettings> element should be included in the program'sApp.config orWeb.config file.
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <addkey="serilog:minimum-level"value="Verbose" /><!-- More settings here-->
Serilog settings are prefixed withserilog:.
To set the logging level for the app use theserilog:minimum-level setting key.
<addkey="serilog:minimum-level"value="Verbose" />
Valid values are those defined in theLogEventLevel enumeration:Verbose,Debug,Information,Warning,Error,Fatal.
Sinks are added with theserilog:write-to key. The setting name matches the configuration method name that you'd use in code, so the following are equivalent:
.WriteTo.Console()
In XML:
<addkey="serilog:write-to:Console" />
NOTE: When usingserilog:* keys need to be unique.
Sink assemblies must be specified using theserilog:using syntax. For example, to configure
<add key="serilog:using:Console"value="Serilog.Sinks.Console"/><add key="serilog:write-to:Console"/>
If the sink accepts parameters, these are specified by appending the parameter name to the setting.
.WriteTo.File(@"C:\Logs\myapp-{Date}.txt",retainedFileCountLimit:10)
In XML:
<addkey="serilog:write-to:File.path"value="C:\Logs\myapp-{Date}.txt" /> <addkey="serilog:write-to:File.retainedFileCountLimit"value="10" />
Any environment variables specified in a setting value (e.g.%TEMP%) will be expanded appropriately when read.
To use sinks and enrichers from additional assemblies, specify them with aserilog:using key.
For example, to use configuration from theSerilog.Sinks.EventLog assembly:
<addkey="serilog:using:EventLog"value="Serilog.Sinks.EventLog" /> <addkey="serilog:write-to:EventLog.source"value="Serilog Demo" />
To attach additional properties to log events, specify them with theserilog:enrich:with-property directive.
For example, to add the propertyRelease with the value"1.2-develop" to all events:
<addkey="serilog:enrich:with-property:Release"value="1.2-develop" />
Since Serilog 2.1,minimum level overrides can be added to change the minimum level for some specific namespaces. This is done with the setting keyserilog:minimum-level:override: followed by thesource context prefix.
For instance, the following are equivalent :
Log.Logger=newLoggerConfiguration().MinimumLevel.Information().MinimumLevel.Override("Microsoft",LogEventLevel.Warning).MinimumLevel.Override("Microsoft.AspNetCore.Mvc",LogEventLevel.Error)
and in XML
<addkey="serilog:minimum-level"value="Information" /> <addkey="serilog:minimum-level:override:Microsoft"value="Warning" /> <addkey="serilog:minimum-level:override:Microsoft.AspNetCore.Mvc"value="Error" />
Filters can be specified using theSerilog.Filters.Expressions package; see theREADME there for more information.
When configuring sink settings with values from enumerations, use the member name, without specifying the name of the enumeration.
For example, to configure theRollingInterval of theFile Sink to create a new log file per day, useDay instead ofRollingInterval.Day:
<addkey="serilog:write-to:File.rollingInterval"value="Day"/>
If you specify the the name of the enumeration, you'll receive an error similar toSystem.ArgumentException: Requested value 'RollingInterval.Day' was not found
LoggerConfiguration.Destructure.ToMaximumDepth(maximumDestructuringDepth:3).Destructure.ToMaximumStringLength(maximumStringLength:3).Destructure.ToMaximumCollectionCount(maximumCollectionCount:3).Destructure.AsScalar(typeof(System.Version)).Destructure.With(newCustomPolicy());
and in XML
<addkey="serilog:destructure:ToMaximumDepth.maximumDestructuringDepth"value="3" /><addkey="serilog:destructure:ToMaximumStringLength.maximumStringLength"value="3" /><addkey="serilog:destructure:ToMaximumCollectionCount.maximumCollectionCount"value="3" /><addkey="serilog:destructure:AsScalar.scalarType"value="System.Version" /><addkey="serilog:destructure:With.policy"value="My.CustomPolicy, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
About
An <appSettings> configuration reader for Serilog
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.