https://github.com/serilog/serilog-settings-appsettings
An <appSettings> configuration reader for Serilog
https://github.com/serilog/serilog-settings-appsettings
Last synced: 3 months ago
JSON representation
An <appSettings> configuration reader for Serilog
- Host: GitHub
- URL: https://github.com/serilog/serilog-settings-appsettings
- Owner: serilog
- License: apache-2.0
- Created: 2016-05-05T01:13:49.000Z (about 9 years ago)
- Default Branch: dev
- Last Pushed: 2024-07-04T01:11:04.000Z (12 months ago)
- Last Synced: 2025-03-29T04:11:15.438Z (3 months ago)
- Language: C#
- Size: 108 KB
- Stars: 54
- Watchers: 12
- Forks: 23
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# Serilog.Settings.AppSettings [](https://ci.appveyor.com/project/serilog/serilog-settings-appsettings) [](https://www.nuget.org/packages/Serilog.Settings.AppSettings/) [](https://gitter.im/serilog/serilog)
An XML `` reader for [Serilog](https://serilog.net).
### Getting started
The `` support package needs to be installed from NuGet:
```powershell
Install-Package Serilog.Settings.AppSettings
```To read configuration from `` use the `ReadFrom.AppSettings()` extension method on your `LoggerConfiguration`:
```csharp
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
... // Other configuration here, then
.CreateLogger()
```You can mix and match XML and code-based configuration, but each sink must be configured **either** using XML **or** in code - sinks added in code can't be modified via app settings.
### Configuration syntax
To configure the logger, an `` element should be included in the program's _App.config_ or _Web.config_ file.
```xml
```Serilog settings are prefixed with `serilog:`.
### Setting the minimum level
To set the logging level for the app use the `serilog:minimum-level` setting key.
```xml
```Valid values are those defined in the `LogEventLevel` enumeration: `Verbose`, `Debug`, `Information`, `Warning`, `Error`, `Fatal`.
### Adding a sink
Sinks are added with the `serilog:write-to` key. The setting name matches the configuration method name that you'd use in code, so the following are equivalent:
```csharp
.WriteTo.Console()
```In XML:
```xml
```**NOTE: When using `serilog:*` keys need to be unique.**
Sink assemblies must be specified using the `serilog:using` syntax. For example, to configure
```csharp
```
If the sink accepts parameters, these are specified by appending the parameter name to the setting.
```csharp
.WriteTo.File(@"C:\Logs\myapp-{Date}.txt", retainedFileCountLimit: 10)
```In XML:
```xml
```Any environment variables specified in a setting value (e.g. `%TEMP%`) will be expanded appropriately when read.
### Using sink extensions from additional assemblies
To use sinks and enrichers from additional assemblies, specify them with a `serilog:using` key.
For example, to use configuration from the `Serilog.Sinks.EventLog` assembly:
```xml
```### Enriching with properties
To attach additional properties to log events, specify them with the `serilog:enrich:with-property` directive.
For example, to add the property `Release` with the value `"1.2-develop"` to all events:
```xml
```### Adding minimum level overrides
Since Serilog 2.1, [minimum level overrides](https://nblumhardt.com/2016/07/serilog-2-minimumlevel-override/) can be added to change the minimum level for some specific namespaces. This is done with the setting key `serilog:minimum-level:override:` followed by the *source context prefix*.
For instance, the following are equivalent :
```csharp
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Error)
```and in XML
```xml
```### Filtering
Filters can be specified using the _Serilog.Filters.Expressions_ package; see the [README](https://github.com/serilog/serilog-filters-expressions) there for more information.
### Setting values from enumerations
When configuring sink settings with values from enumerations, use the member name, without specifying the name of the enumeration.
For example, to configure the `RollingInterval` of the [File Sink](https://github.com/serilog/serilog-sinks-file) to create a new log file per day, use `Day` instead of `RollingInterval.Day`:
```xml
```If you specify the the name of the enumeration, you'll receive an error similar to `System.ArgumentException: Requested value 'RollingInterval.Day' was not found`
### Destructuring
```csharp
LoggerConfiguration
.Destructure.ToMaximumDepth(maximumDestructuringDepth: 3)
.Destructure.ToMaximumStringLength(maximumStringLength: 3)
.Destructure.ToMaximumCollectionCount(maximumCollectionCount: 3)
.Destructure.AsScalar(typeof(System.Version))
.Destructure.With(new CustomPolicy());
```and in XML
```xml
```