-
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.
logging: Explode LoggingHandler into LoggingPreHandler, LoggingPostHa…
…ndler
- Loading branch information
1 parent
7c8c1bc
commit 8c272b0
Showing
5 changed files
with
145 additions
and
84 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
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,41 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using rm.DelegatingHandlers.Formatting; | ||
using Serilog; | ||
|
||
namespace rm.DelegatingHandlers; | ||
|
||
/// <summary> | ||
/// Logs <see cref="HttpRequestMessage"/>. | ||
/// </summary> | ||
public class LoggingPreHandler : DelegatingHandler | ||
{ | ||
private readonly ILogger logger; | ||
private readonly ILoggingFormatter loggingFormatter; | ||
|
||
/// <inheritdoc cref="LoggingPreHandler" /> | ||
public LoggingPreHandler( | ||
ILogger logger, | ||
ILoggingFormatter loggingFormatter) | ||
{ | ||
this.logger = logger?.ForContext(GetType()) | ||
?? throw new ArgumentNullException(nameof(logger)); | ||
this.loggingFormatter = loggingFormatter | ||
?? throw new ArgumentNullException(nameof(loggingFormatter)); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
ILogger lRequest = logger; | ||
lRequest = await lRequest.ForContextAsync(request, loggingFormatter) | ||
.ConfigureAwait(false); | ||
lRequest.Information("request/"); | ||
|
||
return await base.SendAsync(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
} |
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
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
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,80 @@ | ||
using System.Text; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using rm.DelegatingHandlers; | ||
using rm.DelegatingHandlers.Formatting; | ||
using Serilog; | ||
using Serilog.Events; | ||
using Serilog.Formatting.Json; | ||
using Serilog.Sinks.InMemory; | ||
using Serilog.Sinks.InMemory.Assertions; | ||
|
||
namespace rm.DelegatingHandlersTest; | ||
|
||
[TestFixture] | ||
public class LoggingPreHandlerTests | ||
{ | ||
public class Formatter | ||
{ | ||
[Test] | ||
public async Task Logs_Request() | ||
{ | ||
var logger = new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.WriteTo.InMemory() | ||
.WriteTo.Console(new JsonFormatter()) | ||
.CreateLogger(); | ||
var version = "2.0"; | ||
var method = HttpMethod.Post; | ||
var uri = "/health"; | ||
var requestContent = "woot?!"; | ||
var encoding = Encoding.UTF8; | ||
var mimeType = MediaTypeNames.Application.Json; | ||
var header1 = "header1"; | ||
var headerValue1 = "headerValue1"; | ||
var header2 = "header02"; | ||
var headerValue2 = "headerValue02"; | ||
var property1 = "property1"; | ||
var propertyValue1 = "propertyValue1"; | ||
var property2 = "property02"; | ||
var propertyValue2 = "propertyValue02"; | ||
using var request = | ||
new HttpRequestMessage(method, uri) | ||
{ | ||
Version = new Version(version), | ||
Content = new StringContent(requestContent, encoding, mimeType), | ||
}; | ||
request.Headers.Add(header1, headerValue1); | ||
request.Headers.Add(header2, headerValue2); | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
request.Properties.Add(property1, propertyValue1); | ||
request.Properties.Add(property2, propertyValue2); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
var shortCircuitingCannedResponseHandler = new ShortCircuitingCannedResponseHandler(new HttpResponseMessage()); | ||
var loggingPreHandler = new LoggingPreHandler(logger, new LoggingFormatter()); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
loggingPreHandler, shortCircuitingCannedResponseHandler); | ||
|
||
using var _ = await invoker.SendAsync(request, CancellationToken.None); | ||
|
||
InMemorySink.Instance.Should() | ||
.HaveMessage("request/").Appearing().Once() | ||
.WithLevel(LogEventLevel.Information) | ||
.WithProperty($"request.Version").WithValue(version) | ||
.And.WithProperty($"request.HttpMethod").WithValue(method.ToString()) | ||
.And.WithProperty($"request.Uri").WithValue(uri) | ||
.And.WithProperty($"request.Header.{header1}").WithValue(headerValue1) | ||
.And.WithProperty($"request.Header.{header2}").WithValue(headerValue2) | ||
.And.WithProperty($"request.Content.Header.Content-Type").WithValue($"{mimeType}; charset={encoding.BodyName}") | ||
#if NET6_0_OR_GREATER | ||
.And.WithProperty($"request.Content.Header.Content-Length").WithValue(requestContent.Length.ToString()) | ||
#endif | ||
.And.WithProperty($"request.Content").WithValue(requestContent) | ||
.And.WithProperty($"request.Property.{property1}").WithValue(propertyValue1) | ||
.And.WithProperty($"request.Property.{property2}").WithValue(propertyValue2) | ||
; | ||
} | ||
} | ||
} |