|
1 |
| -# serilog-framework-configuration |
2 |
| -A Serilog configuration provider that reads from Microsoft.Framework.Configuration |
| 1 | +# Serilog.Settings.Configuration |
| 2 | + |
| 3 | +A Serilog settings provider that reads from Microsoft.Extensions.Configuration, i.e. .NET Core's `appsettings.json` file. |
| 4 | + |
| 5 | +Configuration is read from the `Serilog` section. |
| 6 | + |
| 7 | +```json |
| 8 | +{ |
| 9 | + "Serilog": { |
| 10 | + "Using": ["Serilog.Sinks.Literate"], |
| 11 | + "MinimumLevel": "Debug", |
| 12 | + "WriteTo": [ |
| 13 | + { "Name": "LiterateConsole" }, |
| 14 | + { "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } } |
| 15 | + ], |
| 16 | + "Enrich": { |
| 17 | + "With": [ |
| 18 | + "FromLogContext", |
| 19 | + "WithMachineName", |
| 20 | + "WithThreadId" |
| 21 | + ], |
| 22 | + "WithProperties": { |
| 23 | + "Application": "Sample" |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +This example relies on the _Serilog.Sinks.Literate_, _Serilog.Sinks.File_, _Serilog.Enrichers.Environment_ and _Serilog.Sinks.Thread_ packages also being installed. |
| 31 | + |
| 32 | +After installing this package, use `ReadFrom.Configuration()` and pass an `IConfiguration` object. |
| 33 | + |
| 34 | +```csharp |
| 35 | +public class Program |
| 36 | +{ |
| 37 | + public static void Main(string[] args) |
| 38 | + { |
| 39 | + var configuration = new ConfigurationBuilder() |
| 40 | + .AddJsonFile("appsettings.json") |
| 41 | + .Build(); |
| 42 | + |
| 43 | + var logger = new LoggerConfiguration() |
| 44 | + .ReadFrom.Configuration(configuration) |
| 45 | + .CreateLogger(); |
| 46 | + |
| 47 | + logger.Information("Hello, world!"); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The `WriteTo` and `Enrich.With` sections support the same syntax, for example the following is valid if no arguments are needed by the sinks: |
| 53 | + |
| 54 | +```json |
| 55 | +"WriteTo": ["LiterateConsole", "DiagnosticTrace"] |
| 56 | +``` |
| 57 | + |
| 58 | +Or alternatively, the long-form (`"Name":` ...) sytax from the first example can be used when arguments need to be supplied. |
| 59 | + |
| 60 | +(This package implements a convention using `ILibraryManager` to find any package with `Serilog` anywhere in the name and pulls configuration methods from it, so the `Using` example above is redundant.) |
0 commit comments