-
Notifications
You must be signed in to change notification settings - Fork 3
/
nuget.txt
86 lines (68 loc) · 3.01 KB
/
nuget.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Serilog.Settings.Reloader
**Serilog.Settings.Reloader** provides completely reloadable settings for Serilog.
Links:
* Github ([Serilog.Settings.Reloader](https://github.com/tagcode/serilog-settings-reloader))
* Nuget ([Serilog.Settings.Reloader](https://www.nuget.org/packages/Serilog.Settings.Reloader))
* License ([Apache-2.0 license](http://www.apache.org/licenses/LICENSE-2.0))
**SwitchableLogger** is assigned with new root *ILogger* when configuration is modified.
```C#
// Create switchable
SwitchableLogger switchableLogger = new SwitchableLogger();
// Assign SwitchableLogger to Serilog.Log.Logger
Serilog.Log.Logger = switchableLogger;
// Assign logger to switchableLogger
switchableLogger.Logger = new Serilog.LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
// Create logger
ILogger logger = Serilog.Log.ForContext<Program>();
// Write
logger.Information("Hello World");
// Reconfigure
ILogger newLogger = new Serilog.LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(outputTemplate: "[{SourceContext}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
// Assign new logger
switchableLogger.Set(newLogger, disposePrev: true);
// Write with the previous logger instance, but with different settings
logger.Information("Hello world again");
```
**.AddSerilogConfigurationLoader()** can be used with dependency injection's *ILoggingBuilder*.
```C#
// Create switchable logger
SwitchableLogger switchableLogger = new SwitchableLogger();
// Read configuration
IConfigurationRoot configuration = new ConfigurationBuilder()
Add(config)
.Build();
// Service collection
IServiceCollection serviceCollection = new ServiceCollection()
.AddLogging(loggingBuilder =>
loggingBuilder
.AddSerilog(switchableLogger, true)
.AddSerilogConfigurationLoader(configuration, switchableLogger)
);
// Services
using (var services = serviceCollection.BuildServiceProvider())
{
// Create logger
Microsoft.Extensions.Logging.ILogger logger = services.GetService<Microsoft.Extensions.Logging.ILogger<Program>>();
// Write
logger.LogInformation("Hello World");
// Modify config
config.Set("Serilog:WriteTo:0:Args:OutputTemplate", "[{SourceContext}] {Message:lj}{NewLine}{Exception}");
configuration.Reload();
// Write with the previous logger instance, but with different settings
logger.LogInformation("Hello world again");
}
```
**.AddSerilogConfigurationLoader(IConfiguration, SwitchableLogger, Func<IConfiguration, ILogger>)** third argument specifies load function.
```C#
loggingBuilder
.AddSerilog(switchableLogger, true)
.AddSerilogConfigurationLoader(configuration, switchableLogger,
c => new Serilog.LoggerConfiguration().ReadFrom.Configuration(c).CreateLogger())
);
```