File tree Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change 11<Project >
22 <PropertyGroup >
3- <Version >4.13.1 </Version >
3+ <Version >4.14.0 </Version >
44 </PropertyGroup >
55</Project >
Original file line number Diff line number Diff line change 22using Microsoft . Extensions . Logging ;
33using Microsoft . Extensions . Logging . Abstractions ;
44using System ;
5+ using System . Collections . Concurrent ;
56
67namespace Checkout
78{
8-
99 public static class LogProvider
1010 {
11+ private static readonly object SyncRoot = new object ( ) ;
1112 private static ILoggerFactory _loggerFactory = new LoggerFactory ( ) ;
13+ private static readonly ConcurrentDictionary < Type , ILogger > Loggers = new ConcurrentDictionary < Type , ILogger > ( ) ;
1214
1315 public static void SetLogFactory ( ILoggerFactory factory )
1416 {
15- _loggerFactory ? . Dispose ( ) ;
16- _loggerFactory = factory ?? new LoggerFactory ( ) ;
17+ lock ( SyncRoot )
18+ {
19+ _loggerFactory ? . Dispose ( ) ;
20+ _loggerFactory = factory ?? new LoggerFactory ( ) ;
21+ Loggers . Clear ( ) ;
22+ }
1723 }
1824
1925 public static ILogger GetLogger ( Type loggerType )
2026 {
21- return loggerType is null ? NullLogger . Instance : _loggerFactory . CreateLogger ( loggerType ) ?? NullLogger . Instance ; ;
27+ if ( loggerType == null )
28+ {
29+ return NullLogger . Instance ;
30+ }
31+
32+ return Loggers . GetOrAdd ( loggerType , type =>
33+ {
34+ lock ( SyncRoot )
35+ {
36+ return _loggerFactory . CreateLogger ( type ) ?? NullLogger . Instance ;
37+ }
38+ } ) ;
2239 }
2340 }
2441}
You can’t perform that action at this time.
0 commit comments