-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dh: Add ServicePointOnExceptionLogging handler
- Loading branch information
1 parent
1a82a40
commit 06cd589
Showing
4 changed files
with
152 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/rm.DelegatingHandlers/ServicePointOnExceptionLoggingHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Serilog; | ||
|
||
namespace rm.DelegatingHandlers; | ||
|
||
/// <summary> | ||
/// Logs <see cref="ServicePoint"/> stats for <see cref="HttpRequestMessage.RequestUri"/> with exception. | ||
/// </summary> | ||
public class ServicePointOnExceptionLoggingHandler : DelegatingHandler | ||
{ | ||
private readonly ILogger logger; | ||
|
||
/// <inheritdoc cref="ServicePointOnExceptionLoggingHandler" /> | ||
public ServicePointOnExceptionLoggingHandler( | ||
ILogger logger) | ||
{ | ||
this.logger = logger?.ForContext(GetType()) | ||
?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
return await base.SendAsync(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var servicePoint = ServicePointManager.FindServicePoint(request.RequestUri); | ||
var props = ServicePointHelpers.GetServicePointEnrichers(servicePoint); | ||
|
||
logger.ForContext(props) | ||
.Error(ex, "ServicePoint stats"); | ||
|
||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/rm.DelegatingHandlersTest/ServicePointOnExceptionLoggingHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Net.Http; | ||
using AutoFixture; | ||
using AutoFixture.AutoMoq; | ||
using Moq; | ||
using NUnit.Framework; | ||
using rm.DelegatingHandlers; | ||
using Serilog; | ||
using Serilog.Core; | ||
|
||
namespace rm.DelegatingHandlersTest; | ||
|
||
[TestFixture] | ||
public class ServicePointOnExceptionLoggingHandlerTests | ||
{ | ||
[Test] | ||
public async Task Does_Not_Log_ServicePoint_Stats() | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
|
||
var loggerMock = fixture.Freeze<Mock<ILogger>>(); | ||
loggerMock.Setup(x => x.ForContext(It.IsAny<Type>())).Returns(loggerMock.Object); | ||
loggerMock.Setup(x => x.ForContext(It.IsAny<IEnumerable<ILogEventEnricher>>())).Returns(loggerMock.Object); | ||
var servicePointOnExceptionLoggingHandler = new ServicePointOnExceptionLoggingHandler(loggerMock.Object); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
fixture.Create<HttpMessageHandler>(), servicePointOnExceptionLoggingHandler); | ||
|
||
using var requestMessage = fixture.Create<HttpRequestMessage>(); | ||
using var _ = await invoker.SendAsync(requestMessage, CancellationToken.None); | ||
|
||
loggerMock.Verify((x) => | ||
x.Error(It.IsAny<string>()), | ||
Times.Never); | ||
} | ||
|
||
[Test] | ||
public void Logs_ServicePoint_Stats_During_Exception() | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
|
||
var loggerMock = fixture.Freeze<Mock<ILogger>>(); | ||
loggerMock.Setup(x => x.ForContext(It.IsAny<Type>())).Returns(loggerMock.Object); | ||
loggerMock.Setup(x => x.ForContext(It.IsAny<IEnumerable<ILogEventEnricher>>())).Returns(loggerMock.Object); | ||
var servicePointOnExceptionLoggingHandler = new ServicePointOnExceptionLoggingHandler(loggerMock.Object); | ||
var throwingHandler = new ThrowingHandler(new TurnDownForWhatException()); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
fixture.Create<HttpMessageHandler>(), servicePointOnExceptionLoggingHandler, throwingHandler); | ||
|
||
using var requestMessage = fixture.Create<HttpRequestMessage>(); | ||
var ex = Assert.ThrowsAsync<TurnDownForWhatException>(async () => | ||
{ | ||
using var _ = await invoker.SendAsync(requestMessage, CancellationToken.None); | ||
}); | ||
|
||
loggerMock.Verify((x) => | ||
x.Error(ex, "ServicePoint stats"), | ||
Times.Once); | ||
} | ||
} |