5
5
using System . Collections . Concurrent ;
6
6
using System . Collections . Generic ;
7
7
using System . Diagnostics ;
8
+ using System . Diagnostics . CodeAnalysis ;
8
9
using System . Linq ;
9
10
using System . Net . Http ;
10
11
using System . Threading ;
@@ -208,7 +209,7 @@ internal void ExpiryTimer_Tick(object? state)
208
209
var expired = new ExpiredHandlerTrackingEntry ( active ) ;
209
210
_expiredHandlers . Enqueue ( expired ) ;
210
211
211
- Log . HandlerExpired ( _logger . Value , active . Name , active . Lifetime ) ;
212
+ Log . HandlerExpired ( _logger , active . Name , active . Lifetime ) ;
212
213
213
214
StartCleanupTimer ( ) ;
214
215
}
@@ -266,7 +267,7 @@ internal void CleanupTimer_Tick()
266
267
try
267
268
{
268
269
int initialCount = _expiredHandlers . Count ;
269
- Log . CleanupCycleStart ( _logger . Value , initialCount ) ;
270
+ Log . CleanupCycleStart ( _logger , initialCount ) ;
270
271
271
272
var stopwatch = ValueStopwatch . StartNew ( ) ;
272
273
@@ -287,7 +288,7 @@ internal void CleanupTimer_Tick()
287
288
}
288
289
catch ( Exception ex )
289
290
{
290
- Log . CleanupItemFailed ( _logger . Value , entry . Name , ex ) ;
291
+ Log . CleanupItemFailed ( _logger , entry . Name , ex ) ;
291
292
}
292
293
}
293
294
else
@@ -298,7 +299,7 @@ internal void CleanupTimer_Tick()
298
299
}
299
300
}
300
301
301
- Log . CleanupCycleEnd ( _logger . Value , stopwatch . GetElapsedTime ( ) , disposedCount , _expiredHandlers . Count ) ;
302
+ Log . CleanupCycleEnd ( _logger , stopwatch . GetElapsedTime ( ) , disposedCount , _expiredHandlers . Count ) ;
302
303
}
303
304
finally
304
305
{
@@ -343,24 +344,48 @@ public static class EventIds
343
344
"HttpMessageHandler expired after {HandlerLifetime}ms for client '{ClientName}'" ) ;
344
345
345
346
346
- public static void CleanupCycleStart ( ILogger logger , int initialCount )
347
+ public static void CleanupCycleStart ( Lazy < ILogger > loggerLazy , int initialCount )
347
348
{
348
- _cleanupCycleStart ( logger , initialCount , null ) ;
349
+ if ( TryGetLogger ( loggerLazy , out ILogger ? logger ) )
350
+ {
351
+ _cleanupCycleStart ( logger , initialCount , null ) ;
352
+ }
353
+ }
354
+
355
+ public static void CleanupCycleEnd ( Lazy < ILogger > loggerLazy , TimeSpan duration , int disposedCount , int finalCount )
356
+ {
357
+ if ( TryGetLogger ( loggerLazy , out ILogger ? logger ) )
358
+ {
359
+ _cleanupCycleEnd ( logger , duration . TotalMilliseconds , disposedCount , finalCount , null ) ;
360
+ }
349
361
}
350
362
351
- public static void CleanupCycleEnd ( ILogger logger , TimeSpan duration , int disposedCount , int finalCount )
363
+ public static void CleanupItemFailed ( Lazy < ILogger > loggerLazy , string clientName , Exception exception )
352
364
{
353
- _cleanupCycleEnd ( logger , duration . TotalMilliseconds , disposedCount , finalCount , null ) ;
365
+ if ( TryGetLogger ( loggerLazy , out ILogger ? logger ) )
366
+ {
367
+ _cleanupItemFailed ( logger , clientName , exception ) ;
368
+ }
354
369
}
355
370
356
- public static void CleanupItemFailed ( ILogger logger , string clientName , Exception exception )
371
+ public static void HandlerExpired ( Lazy < ILogger > loggerLazy , string clientName , TimeSpan lifetime )
357
372
{
358
- _cleanupItemFailed ( logger , clientName , exception ) ;
373
+ if ( TryGetLogger ( loggerLazy , out ILogger ? logger ) )
374
+ {
375
+ _handlerExpired ( logger , lifetime . TotalMilliseconds , clientName , null ) ;
376
+ }
359
377
}
360
378
361
- public static void HandlerExpired ( ILogger logger , string clientName , TimeSpan lifetime )
379
+ private static bool TryGetLogger ( Lazy < ILogger > loggerLazy , [ NotNullWhen ( true ) ] out ILogger ? logger )
362
380
{
363
- _handlerExpired ( logger , lifetime . TotalMilliseconds , clientName , null ) ;
381
+ logger = null ;
382
+ try
383
+ {
384
+ logger = loggerLazy . Value ;
385
+ }
386
+ catch { } // not throwing in logs
387
+
388
+ return logger is not null ;
364
389
}
365
390
}
366
391
}
0 commit comments