Skip to content

Commit

Permalink
dh: Tweak ServicePointLogging handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rmandvikar committed Apr 17, 2024
1 parent 1a82a40 commit c680d3e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
37 changes: 1 addition & 36 deletions src/rm.DelegatingHandlers/ServicePointLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Serilog.Core;
using Serilog.Core.Enrichers;

namespace rm.DelegatingHandlers;

Expand Down Expand Up @@ -36,40 +34,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
finally
{
var servicePoint = ServicePointManager.FindServicePoint(request.RequestUri);

var props =
new ILogEventEnricher[]
{
// global
new PropertyEnricher("CheckCertificateRevocationList", ServicePointManager.CheckCertificateRevocationList),
new PropertyEnricher("DefaultConnectionLimit", ServicePointManager.DefaultConnectionLimit),
new PropertyEnricher("DefaultNonPersistentConnectionLimit", ServicePointManager.DefaultNonPersistentConnectionLimit),
new PropertyEnricher("DefaultPersistentConnectionLimit", ServicePointManager.DefaultPersistentConnectionLimit),
new PropertyEnricher("DnsRefreshTimeout", ServicePointManager.DnsRefreshTimeout),
new PropertyEnricher("EnableDnsRoundRobin", ServicePointManager.EnableDnsRoundRobin),
new PropertyEnricher("EncryptionPolicy", ServicePointManager.EncryptionPolicy),
new PropertyEnricher("Expect100Continue", ServicePointManager.Expect100Continue),
new PropertyEnricher("MaxServicePointIdleTime", ServicePointManager.MaxServicePointIdleTime),
new PropertyEnricher("MaxServicePoints", ServicePointManager.MaxServicePoints),
new PropertyEnricher("ReusePort", ServicePointManager.ReusePort),
new PropertyEnricher("SecurityProtocol", ServicePointManager.SecurityProtocol),
new PropertyEnricher("UseNagleAlgorithm", ServicePointManager.UseNagleAlgorithm),

// servicePoint
new PropertyEnricher("host.Key", $"{servicePoint.Address.Scheme}://{servicePoint.Address.Host}"),
new PropertyEnricher("host.Address", servicePoint.Address),
new PropertyEnricher("host.ConnectionName", servicePoint.ConnectionName),
new PropertyEnricher("host.ProtocolVersion", servicePoint.ProtocolVersion),
new PropertyEnricher("host.Expect100Continue", servicePoint.Expect100Continue),
new PropertyEnricher("host.UseNagleAlgorithm", servicePoint.UseNagleAlgorithm),
new PropertyEnricher("host.SupportsPipelining", servicePoint.SupportsPipelining),
new PropertyEnricher("host.ConnectionLimit", servicePoint.ConnectionLimit),
new PropertyEnricher("host.CurrentConnections", servicePoint.CurrentConnections),
new PropertyEnricher("host.ConnectionLeaseTimeout", servicePoint.ConnectionLeaseTimeout),
new PropertyEnricher("host.IdleSince", servicePoint.IdleSince),
new PropertyEnricher("host.MaxIdleTime", servicePoint.MaxIdleTime),
new PropertyEnricher("host.ReceiveBufferSize", servicePoint.ReceiveBufferSize),
};
var props = ServicePointHelpers.GetServicePointEnrichers(servicePoint);

logger.ForContext(props)
.Information("ServicePoint stats");
Expand Down
46 changes: 46 additions & 0 deletions src/rm.DelegatingHandlers/misc/ServicePointHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Net;
using Serilog.Core;
using Serilog.Core.Enrichers;

namespace rm.DelegatingHandlers;

public static class ServicePointHelpers
{
public static ILogEventEnricher[] GetServicePointEnrichers(ServicePoint servicePoint)
{
return
new[]
{
// global
new PropertyEnricher("CheckCertificateRevocationList", ServicePointManager.CheckCertificateRevocationList),
new PropertyEnricher("DefaultConnectionLimit", ServicePointManager.DefaultConnectionLimit),
new PropertyEnricher("DefaultNonPersistentConnectionLimit", ServicePointManager.DefaultNonPersistentConnectionLimit),
new PropertyEnricher("DefaultPersistentConnectionLimit", ServicePointManager.DefaultPersistentConnectionLimit),
new PropertyEnricher("DnsRefreshTimeout", ServicePointManager.DnsRefreshTimeout),
new PropertyEnricher("EnableDnsRoundRobin", ServicePointManager.EnableDnsRoundRobin),
new PropertyEnricher("EncryptionPolicy", ServicePointManager.EncryptionPolicy),
new PropertyEnricher("Expect100Continue", ServicePointManager.Expect100Continue),
new PropertyEnricher("MaxServicePointIdleTime", ServicePointManager.MaxServicePointIdleTime),
new PropertyEnricher("MaxServicePoints", ServicePointManager.MaxServicePoints),
new PropertyEnricher("ReusePort", ServicePointManager.ReusePort),
new PropertyEnricher("SecurityProtocol", ServicePointManager.SecurityProtocol),
new PropertyEnricher("UseNagleAlgorithm", ServicePointManager.UseNagleAlgorithm),

// servicePoint
// see https://stackoverflow.com/questions/75168666/should-i-pass-the-full-url-or-just-the-domain-to-servicepointmanager-findservice
new PropertyEnricher("host.Key", $"{servicePoint.Address.Scheme}://{servicePoint.Address.DnsSafeHost}"),
new PropertyEnricher("host.Address", servicePoint.Address),
new PropertyEnricher("host.ConnectionName", servicePoint.ConnectionName),
new PropertyEnricher("host.ProtocolVersion", servicePoint.ProtocolVersion),
new PropertyEnricher("host.Expect100Continue", servicePoint.Expect100Continue),
new PropertyEnricher("host.UseNagleAlgorithm", servicePoint.UseNagleAlgorithm),
new PropertyEnricher("host.SupportsPipelining", servicePoint.SupportsPipelining),
new PropertyEnricher("host.ConnectionLimit", servicePoint.ConnectionLimit),
new PropertyEnricher("host.CurrentConnections", servicePoint.CurrentConnections),
new PropertyEnricher("host.ConnectionLeaseTimeout", servicePoint.ConnectionLeaseTimeout),
new PropertyEnricher("host.IdleSince", servicePoint.IdleSince),
new PropertyEnricher("host.MaxIdleTime", servicePoint.MaxIdleTime),
new PropertyEnricher("host.ReceiveBufferSize", servicePoint.ReceiveBufferSize),
};
}
}

0 comments on commit c680d3e

Please sign in to comment.