Skip to content

Commit 5e5b5a2

Browse files
committed
move LogWithThreadPoolStats
1 parent bafb8cb commit 5e5b5a2

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

src/StackExchange.Redis/ConnectionMultiplexer.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -483,43 +483,21 @@ private static async Task<bool> WaitAllIgnoreErrorsAsync(string name, Task[] tas
483483
return true;
484484
}
485485

486-
static void LogWithThreadPoolStats(ILogger? log, string message)
487-
{
488-
if (log?.IsEnabled(LogLevel.Information) != false)
489-
{
490-
return;
491-
}
492-
493-
var busyWorkerCount = 0;
494-
if (log is not null)
495-
{
496-
var sb = new StringBuilder();
497-
sb.Append(message);
498-
busyWorkerCount = PerfCounterHelper.GetThreadPoolStats(out string iocp, out string worker, out string? workItems);
499-
sb.Append(", IOCP: ").Append(iocp).Append(", WORKER: ").Append(worker);
500-
if (workItems is not null)
501-
{
502-
sb.Append(", POOL: ").Append(workItems);
503-
}
504-
log?.LogInformationThreadPoolStats(sb.ToString());
505-
}
506-
}
507-
508486
var watch = ValueStopwatch.StartNew();
509-
LogWithThreadPoolStats(log, $"Awaiting {tasks.Length} {name} task completion(s) for {timeoutMilliseconds}ms");
487+
log.LogWithThreadPoolStats($"Awaiting {tasks.Length} {name} task completion(s) for {timeoutMilliseconds}ms");
510488
try
511489
{
512490
// if none error, great
513491
var remaining = timeoutMilliseconds - watch.ElapsedMilliseconds;
514492
if (remaining <= 0)
515493
{
516-
LogWithThreadPoolStats(log, "Timeout before awaiting for tasks");
494+
log.LogWithThreadPoolStats("Timeout before awaiting for tasks");
517495
return false;
518496
}
519497

520498
var allTasks = Task.WhenAll(tasks).ObserveErrors();
521499
bool all = await allTasks.TimeoutAfter(timeoutMs: remaining).ObserveErrors().ForAwait();
522-
LogWithThreadPoolStats(log, all ? $"All {tasks.Length} {name} tasks completed cleanly" : $"Not all {name} tasks completed cleanly (from {caller}#{callerLineNumber}, timeout {timeoutMilliseconds}ms)");
500+
log.LogWithThreadPoolStats(all ? $"All {tasks.Length} {name} tasks completed cleanly" : $"Not all {name} tasks completed cleanly (from {caller}#{callerLineNumber}, timeout {timeoutMilliseconds}ms)");
523501
return all;
524502
}
525503
catch
@@ -535,7 +513,7 @@ static void LogWithThreadPoolStats(ILogger? log, string message)
535513
var remaining = timeoutMilliseconds - watch.ElapsedMilliseconds;
536514
if (remaining <= 0)
537515
{
538-
LogWithThreadPoolStats(log, "Timeout awaiting tasks");
516+
log.LogWithThreadPoolStats("Timeout awaiting tasks");
539517
return false;
540518
}
541519
try
@@ -546,7 +524,7 @@ static void LogWithThreadPoolStats(ILogger? log, string message)
546524
{ }
547525
}
548526
}
549-
LogWithThreadPoolStats(log, "Finished awaiting tasks");
527+
log.LogWithThreadPoolStats("Finished awaiting tasks");
550528
return false;
551529
}
552530

src/StackExchange.Redis/LoggerExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net;
3+
using System.Text;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Logging;
56

@@ -23,6 +24,25 @@ internal readonly struct ConfigurationOptionsLogValue(ConfigurationOptions optio
2324
public override string ToString() => options.ToString(includePassword: false);
2425
}
2526

27+
// manual extensions
28+
internal static void LogWithThreadPoolStats(this ILogger? log, string message)
29+
{
30+
if (log is null || !log.IsEnabled(LogLevel.Information))
31+
{
32+
return;
33+
}
34+
35+
var sb = new StringBuilder();
36+
sb.Append(message);
37+
_ = PerfCounterHelper.GetThreadPoolStats(out string iocp, out string worker, out string? workItems);
38+
sb.Append(", IOCP: ").Append(iocp).Append(", WORKER: ").Append(worker);
39+
if (workItems is not null)
40+
{
41+
sb.Append(", POOL: ").Append(workItems);
42+
}
43+
log.LogInformationThreadPoolStats(sb.ToString());
44+
}
45+
2646
// Generated LoggerMessage methods
2747
[LoggerMessage(
2848
Level = LogLevel.Error,

0 commit comments

Comments
 (0)