-
Notifications
You must be signed in to change notification settings - Fork 0
/
WimaLoggerProvider.cs
30 lines (25 loc) · 1.16 KB
/
WimaLoggerProvider.cs
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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
namespace Wima.Log
{
public sealed class WimaLoggerProvider : ILoggerProvider
{
private readonly IDisposable _onChangeToken;
public WimaLoggerConfiguration CurrentConfig { get; private set; }
private readonly ConcurrentDictionary<string, WimaLogger> _loggers = new(StringComparer.OrdinalIgnoreCase);
public WimaLoggerProvider(IOptionsMonitor<WimaLoggerConfiguration> config)
{
CurrentConfig = config.CurrentValue;
_onChangeToken = config.OnChange(updatedConfig => CurrentConfig = updatedConfig);
}
public ILogger CreateLogger(string categoryName) =>
_loggers.GetOrAdd(categoryName, name => new WimaLogger(name, CurrentConfig.LogLevel, CurrentConfig.ShowLevel, CurrentConfig.ShowDateTime, CurrentConfig.ShowLogName, CurrentConfig.DateTimeFormat));
public void Dispose()
{
foreach (var logger in _loggers) logger.Value.Dispose();
_loggers.Clear();
_onChangeToken?.Dispose();
}
}
}