From be34bfc1c3d60377bfe27e15150e8481a6639e2c Mon Sep 17 00:00:00 2001 From: hippy Date: Fri, 15 Sep 2023 19:08:13 -0700 Subject: [PATCH] dh: Fix loggingHandler to re-enrich after every op --- src/rm.DelegatingHandlers/LoggingHandler.cs | 24 ++++++++++++------- .../LoggingHandlerTests.cs | 15 ++++++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/rm.DelegatingHandlers/LoggingHandler.cs b/src/rm.DelegatingHandlers/LoggingHandler.cs index d4f58e6..54abd71 100644 --- a/src/rm.DelegatingHandlers/LoggingHandler.cs +++ b/src/rm.DelegatingHandlers/LoggingHandler.cs @@ -32,10 +32,10 @@ protected override async Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { - ILogger l = logger; - l = await l.ForContextAsync(request, loggingFormatter) + ILogger lRequest = logger; + lRequest = await lRequest.ForContextAsync(request, loggingFormatter) .ConfigureAwait(false); - l.Information("request/"); + lRequest.Information("request/"); var stopwatch = Stopwatch.StartNew(); try @@ -45,10 +45,13 @@ protected override async Task SendAsync( stopwatch.Stop(); - l = await l.ForContextAsync(response, loggingFormatter) + ILogger lResponse = logger; + lResponse = await lResponse.ForContextAsync(request, loggingFormatter) .ConfigureAwait(false); - l = l.ForContext(stopwatch, loggingFormatter); - l.Information("request/response"); + lResponse = await lResponse.ForContextAsync(response, loggingFormatter) + .ConfigureAwait(false); + lResponse = lResponse.ForContext(stopwatch, loggingFormatter); + lResponse.Information("request/response"); return response; } @@ -56,9 +59,12 @@ protected override async Task SendAsync( { stopwatch.Stop(); - l = l.ForContext(ex, loggingFormatter); - l = l.ForContext(stopwatch, loggingFormatter); - l.Information(ex, "request/exception"); + ILogger lException = logger; + lException = await logger.ForContextAsync(request, loggingFormatter) + .ConfigureAwait(false); + lException = lException.ForContext(ex, loggingFormatter); + lException = lException.ForContext(stopwatch, loggingFormatter); + lException.Information(ex, "request/exception"); throw; } diff --git a/tests/rm.DelegatingHandlersTest/LoggingHandlerTests.cs b/tests/rm.DelegatingHandlersTest/LoggingHandlerTests.cs index 9b0a09a..c9af55e 100644 --- a/tests/rm.DelegatingHandlersTest/LoggingHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/LoggingHandlerTests.cs @@ -110,6 +110,8 @@ public async Task Logs_Request_Response() var propertyValue1 = "propertyValue1"; var property2 = "property02"; var propertyValue2 = "propertyValue02"; + var property3 = "property03"; + var propertyValue3 = "propertyValue03"; using var request = new HttpRequestMessage(method, uri) { @@ -137,10 +139,18 @@ public async Task Logs_Request_Response() #endif var shortCircuitingCannedResponseHandler = new ShortCircuitingCannedResponseHandler(response); + var delegateHandler = new DelegateHandler( + postDelegate: (request, response, ct) => + { +#pragma warning disable CS0618 // Type or member is obsolete + request.Properties.Add(property3, propertyValue3); +#pragma warning restore CS0618 // Type or member is obsolete + return Task.FromResult(response); + }); var loggingHandler = new LoggingHandler(logger, new LoggingFormatter()); using var invoker = HttpMessageInvokerFactory.Create( - loggingHandler, shortCircuitingCannedResponseHandler); + loggingHandler, delegateHandler, shortCircuitingCannedResponseHandler); using var _ = await invoker.SendAsync(request, CancellationToken.None); @@ -159,6 +169,7 @@ public async Task Logs_Request_Response() .And.WithProperty($"request.Content").WithValue(requestContent) .And.WithProperty($"request.Property.{property1}").WithValue(propertyValue1) .And.WithProperty($"request.Property.{property2}").WithValue(propertyValue2) + .And.WithProperty($"request.Property.{property3}").WithValue(propertyValue3) .And.WithProperty($"response.Version").WithValue(version) .And.WithProperty($"response.StatusCode").WithValue((int)statusCode) .And.WithProperty($"response.ReasonPhrase").WithValue(statusCode.ToString()) @@ -224,8 +235,8 @@ public async Task Logs_Request_Exception() response.Headers.Add(header2, headerValue2); var swallowingHandler = new SwallowingHandler(ex => ex is TurnDownForWhatException); - var throwingHandler = new ThrowingHandler(new TurnDownForWhatException()); var loggingHandler = new LoggingHandler(logger, new LoggingFormatter()); + var throwingHandler = new ThrowingHandler(new TurnDownForWhatException()); using var invoker = HttpMessageInvokerFactory.Create( swallowingHandler, loggingHandler, throwingHandler);