diff --git a/README.md b/README.md index baaf57c..5bef8c6 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ or in `appsettings.json` file: "Serilog": { "MinimumLevel": "Debug", "Using": [ "Serilog.Enrichers.ClientInfo" ], - "Enrich": [ - "WithClientIp", + "Enrich": [ + "WithClientIp", "WithCorrelationId", - { - "Name": "WithRequestHeader", + { + "Name": "WithRequestHeader", "Args": { "headerName": "User-Agent"} } ], @@ -57,7 +57,7 @@ or "Serilog": { "MinimumLevel": "Debug", "Using": [ "Serilog.Enrichers.ClientInfo" ], - "Enrich": [ + "Enrich": [ { "Name": "WithClientIp", "Args": { @@ -83,7 +83,7 @@ or "Serilog": { "MinimumLevel": "Debug", "Using": [ "Serilog.Enrichers.ClientInfo" ], - "Enrich": [ + "Enrich": [ { "Name": "WithCorrelationId", "Args": { @@ -109,7 +109,7 @@ or "Serilog": { "MinimumLevel": "Debug", "Using": [ "Serilog.Enrichers.ClientInfo" ], - "Enrich": [ + "Enrich": [ { "Name": "WithRequestHeader", "Args": { @@ -121,6 +121,13 @@ or "Args": { "headerName": "Connection" } + }, + { + "Name": "WithRequestHeader", + "Args": { + "headerName": "Content-Length", + "propertyName": "RequestLength" + } } ], } @@ -160,6 +167,7 @@ namespace MyWebApp .Enrich.WithClientIp() .Enrich.WithCorrelationId() .Enrich.WithRequestHeader("header-name") + .Enrich.WithRequestHeader("another-header-name", "some-property-name") .CreateLogger(); } diff --git a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs index 7155a71..579aa61 100644 --- a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs +++ b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs @@ -19,15 +19,17 @@ public class ClientHeaderEnricher : ILogEventEnricher private readonly string _headerKey; private readonly IHttpContextAccessor _contextAccessor; - public ClientHeaderEnricher(string headerKey) - : this(headerKey, new HttpContextAccessor()) + public ClientHeaderEnricher(string headerKey, string propertyName) + : this(headerKey, propertyName, new HttpContextAccessor()) { } - internal ClientHeaderEnricher(string headerKey, IHttpContextAccessor contextAccessor) + internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor) { _headerKey = headerKey; - _propertyName = headerKey.Replace("-", ""); + _propertyName = string.IsNullOrWhiteSpace(propertyName) + ? headerKey.Replace("-", "") + : propertyName; _clientHeaderItemKey = $"Serilog_{headerKey}"; _contextAccessor = contextAccessor; } @@ -57,4 +59,4 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) logEvent.AddPropertyIfAbsent(logProperty); } -} \ No newline at end of file +} diff --git a/src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs b/src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs index 7e24887..0a9a0f0 100644 --- a/src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs +++ b/src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs @@ -73,11 +73,13 @@ public static LoggerConfiguration WithCorrelationId( /// Registers the HTTP request header enricher to enrich logs with the header value. /// /// The enrichment configuration. + /// The property name of log /// The header name to log its value /// enrichmentConfiguration /// headerName /// The logger configuration so that multiple calls can be chained. - public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration, string headerName) + public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration, + string headerName, string propertyName = null) { if (enrichmentConfiguration == null) { @@ -89,6 +91,6 @@ public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfigu throw new ArgumentNullException(nameof(headerName)); } - return enrichmentConfiguration.With(new ClientHeaderEnricher(headerName)); + return enrichmentConfiguration.With(new ClientHeaderEnricher(propertyName, headerName)); } -} \ No newline at end of file +} diff --git a/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs b/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs index 5e03b20..29dd009 100644 --- a/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs +++ b/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs @@ -16,6 +16,33 @@ public ClientHeaderEnricherTests() _contextAccessor = Substitute.For(); _contextAccessor.HttpContext.Returns(httpContext); } + + [Fact] + public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateNamedHeaderValueProperty() + { + // Arrange + var headerKey = "RequestId"; + var propertyName = "HttpRequestId"; + var headerValue = Guid.NewGuid().ToString(); + _contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue); + + var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName, _contextAccessor); + + LogEvent evt = null; + var log = new LoggerConfiguration() + .Enrich.With(clientHeaderEnricher) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + // Act + log.Information(@"First testing log enricher."); + log.Information(@"Second testing log enricher."); + + // Assert + Assert.NotNull(evt); + Assert.True(evt.Properties.ContainsKey(propertyName)); + Assert.Equal(headerValue, evt.Properties[propertyName].LiteralValue().ToString()); + } [Fact] public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty() @@ -25,7 +52,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateH var headerValue = Guid.NewGuid().ToString(); _contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue); - var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor); + var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor); LogEvent evt = null; var log = new LoggerConfiguration() @@ -54,8 +81,8 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead _contextAccessor.HttpContext.Request.Headers.Add(headerKey1, headerValue1); _contextAccessor.HttpContext.Request.Headers.Add(headerKey2, headerValue2); - var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, _contextAccessor); - var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, _contextAccessor); + var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, propertyName:string.Empty, _contextAccessor); + var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, propertyName:string.Empty, _contextAccessor); LogEvent evt = null; var log = new LoggerConfiguration() @@ -81,7 +108,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCrea { // Arrange var headerKey = "RequestId"; - var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor); + var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor); LogEvent evt = null; var log = new LoggerConfiguration() @@ -114,4 +141,4 @@ public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException() // Assert Assert.Null(exception); } -} \ No newline at end of file +}