From 49ce1d750e5070f0b46c9e5472ab0e6026426d54 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sat, 19 Jul 2025 13:09:50 +0200 Subject: [PATCH] Fix KeyNotFoundException in enrichers when accessing httpContext.Items (#56) * Fix KeyNotFoundException in all enrichers by using TryGetValue instead of direct dictionary access ------ Co-authored-by: Mohsen Esmailpour --- .../Enrichers/ClientHeaderEnricher.cs | 28 ++++---- .../Enrichers/ClientIpEnricher.cs | 24 +++---- .../Enrichers/CorrelationIdEnricher.cs | 41 +++++------ .../ClientHeaderEnricherTests.cs | 71 +++++++++++++------ .../ClientIpEnricherTests.cs | 50 ++++++++++--- .../CorrelationIdEnricherTests.cs | 68 ++++++++++-------- 6 files changed, 162 insertions(+), 120 deletions(-) diff --git a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs index 33722be..db3ff35 100644 --- a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs +++ b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs @@ -4,16 +4,16 @@ namespace Serilog.Enrichers; -/// +/// public class ClientHeaderEnricher : ILogEventEnricher { private readonly string _clientHeaderItemKey; - private readonly string _propertyName; - private readonly string _headerKey; private readonly IHttpContextAccessor _contextAccessor; + private readonly string _headerKey; + private readonly string _propertyName; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The key of the header. /// The name of the property. @@ -25,9 +25,7 @@ public ClientHeaderEnricher(string headerKey, string propertyName) internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor) { _headerKey = headerKey; - _propertyName = string.IsNullOrWhiteSpace(propertyName) - ? headerKey.Replace("-", "") - : propertyName; + _propertyName = string.IsNullOrWhiteSpace(propertyName) ? headerKey.Replace("-", "") : propertyName; _clientHeaderItemKey = $"Serilog_{headerKey}"; _contextAccessor = contextAccessor; } @@ -37,25 +35,23 @@ internal ClientHeaderEnricher(IHttpContextAccessor contextAccessor) _contextAccessor = contextAccessor; } - /// + /// public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - var httpContext = _contextAccessor.HttpContext; - if (httpContext == null) - { - return; - } + HttpContext httpContext = _contextAccessor.HttpContext; + if (httpContext == null) return; - if (httpContext.Items[_clientHeaderItemKey] is LogEventProperty logEventProperty) + if (httpContext.Items.TryGetValue(_clientHeaderItemKey, out object value) && + value is LogEventProperty logEventProperty) { logEvent.AddPropertyIfAbsent(logEventProperty); return; } - var headerValue = httpContext.Request.Headers[_headerKey].ToString(); + string headerValue = httpContext.Request.Headers[_headerKey].ToString(); headerValue = string.IsNullOrWhiteSpace(headerValue) ? null : headerValue; - var logProperty = new LogEventProperty(_propertyName, new ScalarValue(headerValue)); + LogEventProperty logProperty = new(_propertyName, new ScalarValue(headerValue)); httpContext.Items.Add(_clientHeaderItemKey, logProperty); logEvent.AddPropertyIfAbsent(logProperty); diff --git a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs index b29f80c..99c4142 100644 --- a/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs +++ b/src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs @@ -1,13 +1,13 @@ -using Microsoft.AspNetCore.Http; +using System.Runtime.CompilerServices; +using Microsoft.AspNetCore.Http; using Serilog.Core; using Serilog.Events; -using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("Serilog.Enrichers.ClientInfo.Tests")] namespace Serilog.Enrichers; -/// +/// public class ClientIpEnricher : ILogEventEnricher { private const string IpAddressPropertyName = "ClientIp"; @@ -16,7 +16,7 @@ public class ClientIpEnricher : ILogEventEnricher private readonly IHttpContextAccessor _contextAccessor; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// public ClientIpEnricher() : this(new HttpContextAccessor()) { @@ -27,23 +27,19 @@ internal ClientIpEnricher(IHttpContextAccessor contextAccessor) _contextAccessor = contextAccessor; } - /// + /// public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - var httpContext = _contextAccessor.HttpContext; - if (httpContext == null) - { - return; - } + HttpContext httpContext = _contextAccessor.HttpContext; + if (httpContext == null) return; - var ipAddress = httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"; + string ipAddress = httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"; - if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty) + if (httpContext.Items.TryGetValue(IpAddressItemKey, out object value) && + value is LogEventProperty logEventProperty) { if (!((ScalarValue)logEventProperty.Value).Value.ToString()!.Equals(ipAddress)) - { logEventProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress)); - } logEvent.AddPropertyIfAbsent(logEventProperty); return; diff --git a/src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs b/src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs index dfa82d1..9930e82 100644 --- a/src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs +++ b/src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs @@ -1,27 +1,28 @@ -using Microsoft.AspNetCore.Http; +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; using Serilog.Core; using Serilog.Events; -using System; namespace Serilog.Enrichers; -/// +/// public class CorrelationIdEnricher : ILogEventEnricher { private const string CorrelationIdItemKey = "Serilog_CorrelationId"; private const string PropertyName = "CorrelationId"; - private readonly string _headerKey; private readonly bool _addValueIfHeaderAbsence; private readonly IHttpContextAccessor _contextAccessor; + private readonly string _headerKey; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// - /// The header key used to retrieve the correlation ID from the HTTP request or response headers. + /// The header key used to retrieve the correlation ID from the HTTP request or response headers. /// /// - /// Determines whether to add a new correlation ID value if the header is absent. + /// Determines whether to add a new correlation ID value if the header is absent. /// public CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence) : this(headerKey, addValueIfHeaderAbsence, new HttpContextAccessor()) @@ -35,44 +36,34 @@ internal CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence, I _contextAccessor = contextAccessor; } - /// + /// public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - var httpContext = _contextAccessor.HttpContext; - if (httpContext == null) - { - return; - } + HttpContext httpContext = _contextAccessor.HttpContext; + if (httpContext == null) return; - if (httpContext.Items[CorrelationIdItemKey] is LogEventProperty logEventProperty) + if (httpContext.Items.TryGetValue(CorrelationIdItemKey, out object value) && + value is LogEventProperty logEventProperty) { logEvent.AddPropertyIfAbsent(logEventProperty); return; } - var requestHeader = httpContext.Request.Headers[_headerKey]; - var responseHeader = httpContext.Response.Headers[_headerKey]; + StringValues requestHeader = httpContext.Request.Headers[_headerKey]; + StringValues responseHeader = httpContext.Response.Headers[_headerKey]; string correlationId; if (!string.IsNullOrWhiteSpace(requestHeader)) - { correlationId = requestHeader; - } else if (!string.IsNullOrWhiteSpace(responseHeader)) - { correlationId = responseHeader; - } else if (_addValueIfHeaderAbsence) - { correlationId = Guid.NewGuid().ToString(); - } else - { correlationId = null; - } - var correlationIdProperty = new LogEventProperty(PropertyName, new ScalarValue(correlationId)); + LogEventProperty correlationIdProperty = new(PropertyName, new ScalarValue(correlationId)); logEvent.AddOrUpdateProperty(correlationIdProperty); httpContext.Items.Add(CorrelationIdItemKey, correlationIdProperty); diff --git a/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs b/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs index 30554b0..02b10d2 100644 --- a/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs +++ b/test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http; using NSubstitute; +using Serilog.Core; using Serilog.Events; using System; using Xunit; @@ -12,7 +13,7 @@ public class ClientHeaderEnricherTests public ClientHeaderEnricherTests() { - var httpContext = new DefaultHttpContext(); + DefaultHttpContext httpContext = new(); _contextAccessor = Substitute.For(); _contextAccessor.HttpContext.Returns(httpContext); } @@ -21,15 +22,15 @@ public ClientHeaderEnricherTests() public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateNamedHeaderValueProperty() { // Arrange - var headerKey = "RequestId"; - var propertyName = "HttpRequestId"; - var headerValue = Guid.NewGuid().ToString(); + string headerKey = "RequestId"; + string propertyName = "HttpRequestId"; + string headerValue = Guid.NewGuid().ToString(); _contextAccessor.HttpContext!.Request!.Headers[headerKey] = headerValue; - var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName, _contextAccessor); + ClientHeaderEnricher clientHeaderEnricher = new(headerKey, propertyName, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(clientHeaderEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); @@ -48,14 +49,14 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateN public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty() { // Arrange - var headerKey = "RequestId"; - var headerValue = Guid.NewGuid().ToString(); + string headerKey = "RequestId"; + string headerValue = Guid.NewGuid().ToString(); _contextAccessor!.HttpContext!.Request!.Headers[headerKey] = headerValue; - var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName: string.Empty, _contextAccessor); + ClientHeaderEnricher clientHeaderEnricher = new(headerKey, string.Empty, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(clientHeaderEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); @@ -71,20 +72,21 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateH } [Fact] - public void EnrichLogWithMultipleClientHeaderEnricher_WhenHttpRequestContainHeaders_ShouldCreateHeaderValuesProperty() + public void + EnrichLogWithMultipleClientHeaderEnricher_WhenHttpRequestContainHeaders_ShouldCreateHeaderValuesProperty() { // Arrange - var headerKey1 = "Header1"; - var headerKey2 = "User-Agent"; - var headerValue1 = Guid.NewGuid().ToString(); - var headerValue2 = Guid.NewGuid().ToString(); + string headerKey1 = "Header1"; + string headerKey2 = "User-Agent"; + string headerValue1 = Guid.NewGuid().ToString(); + string headerValue2 = Guid.NewGuid().ToString(); _contextAccessor!.HttpContext!.Request!.Headers[headerKey1] = headerValue1; _contextAccessor!.HttpContext!.Request!.Headers[headerKey2] = headerValue2; - var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, propertyName: string.Empty, _contextAccessor); - var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, propertyName: string.Empty, _contextAccessor); + ClientHeaderEnricher clientHeaderEnricher1 = new(headerKey1, string.Empty, _contextAccessor); + ClientHeaderEnricher clientHeaderEnricher2 = new(headerKey2, string.Empty, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(clientHeaderEnricher1) .Enrich.With(clientHeaderEnricher2) .WriteTo.Sink(new DelegatingSink(e => evt = e)) @@ -106,11 +108,11 @@ public void EnrichLogWithMultipleClientHeaderEnricher_WhenHttpRequestContainHead public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCreateHeaderValuePropertyWithNoValue() { // Arrange - var headerKey = "RequestId"; - var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName: string.Empty, _contextAccessor); + string headerKey = "RequestId"; + ClientHeaderEnricher clientHeaderEnricher = new(headerKey, string.Empty, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(clientHeaderEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); @@ -125,17 +127,40 @@ public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCrea Assert.Null(evt.Properties[headerKey].LiteralValue()); } + [Fact] + public void EnrichLogWithClientIp_WhenKeyNotInItems_ShouldWorkCorrectly() + { + // Arrange + string headerKey = "x-dummy-header"; + ClientHeaderEnricher clientHeaderEnricher = new(headerKey, string.Empty, _contextAccessor); + + LogEvent evt = null; + Logger log = new LoggerConfiguration() + .Enrich.With(clientHeaderEnricher) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + log.Information("Testing log enricher."); + + // Act - This should work without throwing any exceptions + Exception exception = Record.Exception(() => log.Information("Test log message")); + + // Assert + Assert.Null(exception); + Assert.NotNull(evt); + } + [Fact] public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException() { // Arrange - var logger = new LoggerConfiguration() + Logger logger = new LoggerConfiguration() .Enrich.WithRequestHeader("HeaderName") .WriteTo.Sink(new DelegatingSink(_ => { })) .CreateLogger(); // Act - var exception = Record.Exception(() => logger.Information("LOG")); + Exception exception = Record.Exception(() => logger.Information("LOG")); // Assert Assert.Null(exception); diff --git a/test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs b/test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs index a35d098..9ab9148 100644 --- a/test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs +++ b/test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Http; using NSubstitute; +using Serilog.Core; using Serilog.Events; +using System; using System.Net; using Xunit; @@ -12,7 +14,7 @@ public class ClientIpEnricherTests public ClientIpEnricherTests() { - var httpContext = new DefaultHttpContext(); + DefaultHttpContext httpContext = new(); _contextAccessor = Substitute.For(); _contextAccessor.HttpContext.Returns(httpContext); } @@ -25,19 +27,19 @@ public ClientIpEnricherTests() public void EnrichLogWithClientIp_ShouldCreateClientIpPropertyWithValue(string ip) { // Arrange - var ipAddress = IPAddress.Parse(ip); + IPAddress ipAddress = IPAddress.Parse(ip); _contextAccessor.HttpContext!.Connection.RemoteIpAddress = ipAddress; - var ipEnricher = new ClientIpEnricher(_contextAccessor); + ClientIpEnricher ipEnricher = new(_contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(ipEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has an IP property"); + log.Information("Has an IP property"); // Assert Assert.NotNull(evt); @@ -50,17 +52,17 @@ public void EnrichLogWithClientIp_WhenLogMoreThanOnce_ShouldReadClientIpValueFro { //Arrange _contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback; - var ipEnricher = new ClientIpEnricher(_contextAccessor); + ClientIpEnricher ipEnricher = new(_contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(ipEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has an IP property"); - log.Information(@"Has an other IP property"); + log.Information("Has an IP property"); + log.Information("Has an other IP property"); // Assert Assert.NotNull(evt); @@ -72,15 +74,41 @@ public void EnrichLogWithClientIp_WhenLogMoreThanOnce_ShouldReadClientIpValueFro public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException() { // Arrange - var logger = new LoggerConfiguration() + Logger logger = new LoggerConfiguration() .Enrich.WithClientIp() .WriteTo.Sink(new DelegatingSink(_ => { })) .CreateLogger(); // Act - var exception = Record.Exception(() => logger.Information("LOG")); + Exception exception = Record.Exception(() => logger.Information("LOG")); // Assert Assert.Null(exception); } + + [Fact] + public void EnrichLogWithClientIp_WhenKeyNotInItems_ShouldWorkCorrectly() + { + // Arrange + _contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback; + ClientIpEnricher ipEnricher = new(_contextAccessor); + + // Ensure the Items dictionary doesn't contain our key initially + _contextAccessor.HttpContext.Items.Clear(); + + LogEvent evt = null; + Logger log = new LoggerConfiguration() + .Enrich.With(ipEnricher) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + // Act - This should work without throwing any exceptions + Exception exception = Record.Exception(() => log.Information("Test log message")); + + // Assert + Assert.Null(exception); + Assert.NotNull(evt); + Assert.True(evt.Properties.ContainsKey("ClientIp")); + Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue()); + } } \ No newline at end of file diff --git a/test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs b/test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs index 34c1793..f830339 100644 --- a/test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs +++ b/test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http; using NSubstitute; +using Serilog.Core; using Serilog.Events; using System; using Xunit; @@ -14,7 +15,7 @@ public class CorrelationIdEnricherTests public CorrelationIdEnricherTests() { - var httpContext = new DefaultHttpContext(); + DefaultHttpContext httpContext = new(); _contextAccessor = Substitute.For(); _contextAccessor.HttpContext.Returns(httpContext); } @@ -23,18 +24,18 @@ public CorrelationIdEnricherTests() public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdProperty() { // Arrange - var correlationId = Guid.NewGuid().ToString(); + string correlationId = Guid.NewGuid().ToString(); _contextAccessor.HttpContext!.Request!.Headers[HeaderKey] = correlationId; - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, false, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -43,21 +44,22 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_S } [Fact] - public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdPropertyHasValue() + public void + EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_ShouldCreateCorrelationIdPropertyHasValue() { // Arrange - var correlationId = Guid.NewGuid().ToString(); + string correlationId = Guid.NewGuid().ToString(); _contextAccessor.HttpContext!.Request!.Headers[HeaderKey] = correlationId; - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, false, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -66,19 +68,20 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_S } [Fact] - public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsFalse_ShouldCreateCorrelationIdPropertyWithNoValue() + public void + EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsFalse_ShouldCreateCorrelationIdPropertyWithNoValue() { // Arrange - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, false, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -87,19 +90,20 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade } [Fact] - public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsTrue_ShouldCreateCorrelationIdPropertyHasValue() + public void + EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeaderAndAddDefaultValueIsTrue_ShouldCreateCorrelationIdPropertyHasValue() { // Arrange - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, true, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, true, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -108,21 +112,22 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade } [Fact] - public void EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHeader_ShouldCreateCorrelationIdProperty() + public void + EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHeader_ShouldCreateCorrelationIdProperty() { // Arrange - var correlationId = Guid.NewGuid().ToString(); + string correlationId = Guid.NewGuid().ToString(); _contextAccessor.HttpContext!.Request!.Headers[HeaderKey] = correlationId; - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, false, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -131,23 +136,24 @@ public void EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHead } [Fact] - public void EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelationIdHeader_ShouldCreateCorrelationIdPropertyFromHttpRequest() + public void + EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelationIdHeader_ShouldCreateCorrelationIdPropertyFromHttpRequest() { // Arrange - var requestCorrelationId = Guid.NewGuid().ToString(); - var responseCorrelationId = Guid.NewGuid().ToString(); + string requestCorrelationId = Guid.NewGuid().ToString(); + string responseCorrelationId = Guid.NewGuid().ToString(); _contextAccessor.HttpContext!.Request!.Headers[HeaderKey] = requestCorrelationId; _contextAccessor.HttpContext!.Response!.Headers[HeaderKey] = responseCorrelationId; - var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor); + CorrelationIdEnricher correlationIdEnricher = new(HeaderKey, false, _contextAccessor); LogEvent evt = null; - var log = new LoggerConfiguration() + Logger log = new LoggerConfiguration() .Enrich.With(correlationIdEnricher) .WriteTo.Sink(new DelegatingSink(e => evt = e)) .CreateLogger(); // Act - log.Information(@"Has a correlation id."); + log.Information("Has a correlation id."); // Assert Assert.NotNull(evt); @@ -159,13 +165,13 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelat public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException() { // Arrange - var logger = new LoggerConfiguration() + Logger logger = new LoggerConfiguration() .Enrich.WithCorrelationId() .WriteTo.Sink(new DelegatingSink(_ => { })) .CreateLogger(); // Act - var exception = Record.Exception(() => logger.Information("LOG")); + Exception exception = Record.Exception(() => logger.Information("LOG")); // Assert Assert.Null(exception);