From a465ec7c8167f819e4fcb8d14ef7187da619cc1b Mon Sep 17 00:00:00 2001 From: liviriniu Date: Mon, 20 Apr 2020 16:11:52 +0300 Subject: [PATCH 1/4] Fixed bug reported in #67 - Request accessed directly from current context --- .../Classic/Enrichers/HttpRequestUrlEnricher.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs index c75e032..c27ce82 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs @@ -13,7 +13,6 @@ // limitations under the License. using System; -using System.Web; using Serilog.Core; using Serilog.Events; @@ -41,7 +40,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { if (logEvent == null) throw new ArgumentNullException("logEvent"); - if (HttpContext.Current?.Request?.Url == null) + if (HttpContextCurrent.Request?.Url == null) return; var requestUrl = HttpContextCurrent.Request.Url.ToString(); From 708aad21174892a2d76227cb233dc797c2d60a5c Mon Sep 17 00:00:00 2001 From: liviriniu Date: Mon, 20 Apr 2020 16:33:28 +0300 Subject: [PATCH 2/4] Simplified the Request getter --- .../Classic/Enrichers/HttpContextCurrent.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs index 7f3806c..6611f95 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs @@ -23,12 +23,9 @@ internal static HttpRequest Request { get { - HttpContext httpContext = HttpContext.Current; - if (httpContext == null) - return null; try { - return httpContext.Request; + return HttpContext.Current?.Request; } catch (HttpException) { From c4ae6364598aac795c78a186755a8017ccd4a970 Mon Sep 17 00:00:00 2001 From: liviriniu Date: Mon, 20 Apr 2020 16:40:53 +0300 Subject: [PATCH 3/4] renamed HttpContextCurrent class to CurrentHttpContext to avoid confusion with HttpContext.Current --- .../Classic/Enrichers/ClaimValueEnricher.cs | 2 +- .../{HttpContextCurrent.cs => CurrentHttpContext.cs} | 2 +- .../Enrichers/HttpRequestClientHostIPEnricher.cs | 12 ++++++------ .../Enrichers/HttpRequestClientHostNameEnricher.cs | 6 +++--- .../Classic/Enrichers/HttpRequestRawUrlEnricher.cs | 6 +++--- .../Classic/Enrichers/HttpRequestTypeEnricher.cs | 6 +++--- .../Classic/Enrichers/HttpRequestUrlEnricher.cs | 4 ++-- .../Enrichers/HttpRequestUrlReferrerEnricher.cs | 6 +++--- .../Enrichers/HttpRequestUserAgentEnricher.cs | 6 +++--- .../Classic/HttpApplicationWrapper.cs | 2 +- src/SerilogWeb.Classic/SerilogWeb.Classic.csproj | 2 +- 11 files changed, 27 insertions(+), 27 deletions(-) rename src/SerilogWeb.Classic/Classic/Enrichers/{HttpContextCurrent.cs => CurrentHttpContext.cs} (97%) diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/ClaimValueEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/ClaimValueEnricher.cs index 0960b2b..12aa97e 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/ClaimValueEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/ClaimValueEnricher.cs @@ -70,7 +70,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (HttpContext.Current == null) return; - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; var user = HttpContext.Current.User; diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs b/src/SerilogWeb.Classic/Classic/Enrichers/CurrentHttpContext.cs similarity index 97% rename from src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs rename to src/SerilogWeb.Classic/Classic/Enrichers/CurrentHttpContext.cs index 6611f95..54f3814 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/CurrentHttpContext.cs @@ -7,7 +7,7 @@ namespace SerilogWeb.Classic.Enrichers /// This helper class is used to handle special case introduced by ASP.NET integrated pipeline /// when HttpContextCurrent.Request may throw instead of returning null. /// - static class HttpContextCurrent + static class CurrentHttpContext { /// /// Gets the object for the current HTTP request. diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostIPEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostIPEnricher.cs index bdde046..9aea12d 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostIPEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostIPEnricher.cs @@ -71,10 +71,10 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (HttpContext.Current == null) return; - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (string.IsNullOrWhiteSpace(HttpContextCurrent.Request.UserHostAddress)) + if (string.IsNullOrWhiteSpace(CurrentHttpContext.Request.UserHostAddress)) return; string userHostAddress; @@ -82,13 +82,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) // Taking Proxy/-ies into consideration, too (if wanted and available) if (CheckForHttpProxies) { - userHostAddress = !string.IsNullOrWhiteSpace(HttpContextCurrent.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) - ? HttpContextCurrent.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] - : HttpContextCurrent.Request.UserHostAddress; + userHostAddress = !string.IsNullOrWhiteSpace(CurrentHttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) + ? CurrentHttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] + : CurrentHttpContext.Request.UserHostAddress; } else { - userHostAddress = HttpContextCurrent.Request.UserHostAddress; + userHostAddress = CurrentHttpContext.Request.UserHostAddress; } if (string.IsNullOrWhiteSpace(userHostAddress)) diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostNameEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostNameEnricher.cs index 4f10eac..8c334fc 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostNameEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestClientHostNameEnricher.cs @@ -39,13 +39,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { if (logEvent == null) throw new ArgumentNullException("logEvent"); - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (string.IsNullOrWhiteSpace(HttpContextCurrent.Request.UserHostName)) + if (string.IsNullOrWhiteSpace(CurrentHttpContext.Request.UserHostName)) return; - var userHostName = HttpContextCurrent.Request.UserHostName; + var userHostName = CurrentHttpContext.Request.UserHostName; var httpRequestClientHostnameProperty = new LogEventProperty(HttpRequestClientHostNamePropertyName, new ScalarValue(userHostName)); logEvent.AddPropertyIfAbsent(httpRequestClientHostnameProperty); } diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestRawUrlEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestRawUrlEnricher.cs index fec76b5..e249029 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestRawUrlEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestRawUrlEnricher.cs @@ -43,13 +43,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (HttpContext.Current == null) return; - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (string.IsNullOrWhiteSpace(HttpContextCurrent.Request.RawUrl)) + if (string.IsNullOrWhiteSpace(CurrentHttpContext.Request.RawUrl)) return; - var requestRawUrl = HttpContextCurrent.Request.RawUrl; + var requestRawUrl = CurrentHttpContext.Request.RawUrl; var httpRequestRawUrlProperty = new LogEventProperty(HttpRequestRawUrlPropertyName, new ScalarValue(requestRawUrl)); logEvent.AddPropertyIfAbsent(httpRequestRawUrlProperty); } diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestTypeEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestTypeEnricher.cs index 3193309..5c532f2 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestTypeEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestTypeEnricher.cs @@ -43,13 +43,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (HttpContext.Current == null) return; - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (string.IsNullOrWhiteSpace(HttpContextCurrent.Request.RequestType)) + if (string.IsNullOrWhiteSpace(CurrentHttpContext.Request.RequestType)) return; - var requestType = HttpContextCurrent.Request.RequestType; + var requestType = CurrentHttpContext.Request.RequestType; var httpRequestTypeProperty = new LogEventProperty(HttpRequestTypePropertyName, new ScalarValue(requestType)); logEvent.AddPropertyIfAbsent(httpRequestTypeProperty); } diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs index c27ce82..d550810 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlEnricher.cs @@ -40,10 +40,10 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { if (logEvent == null) throw new ArgumentNullException("logEvent"); - if (HttpContextCurrent.Request?.Url == null) + if (CurrentHttpContext.Request?.Url == null) return; - var requestUrl = HttpContextCurrent.Request.Url.ToString(); + var requestUrl = CurrentHttpContext.Request.Url.ToString(); var httpRequestUrlProperty = new LogEventProperty(HttpRequestUrlPropertyName, new ScalarValue(requestUrl)); logEvent.AddPropertyIfAbsent(httpRequestUrlProperty); } diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs index 3449acd..d2c76c7 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs @@ -43,13 +43,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (HttpContext.Current == null) return; - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (HttpContextCurrent.Request.UrlReferrer == null) + if (CurrentHttpContext.Request.UrlReferrer == null) return; - var requestUrlReferrer = HttpContextCurrent.Request.UrlReferrer.ToString(); + var requestUrlReferrer = CurrentHttpContext.Request.UrlReferrer.ToString(); var httpRequestUrlReferrerProperty = new LogEventProperty(HttpRequestUrlReferrerPropertyName, new ScalarValue(requestUrlReferrer)); logEvent.AddPropertyIfAbsent(httpRequestUrlReferrerProperty); } diff --git a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUserAgentEnricher.cs b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUserAgentEnricher.cs index f49a94d..c1fa51a 100644 --- a/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUserAgentEnricher.cs +++ b/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUserAgentEnricher.cs @@ -40,13 +40,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { if (logEvent == null) throw new ArgumentNullException("logEvent"); - if (HttpContextCurrent.Request == null) + if (CurrentHttpContext.Request == null) return; - if (string.IsNullOrWhiteSpace(HttpContextCurrent.Request.UserAgent)) + if (string.IsNullOrWhiteSpace(CurrentHttpContext.Request.UserAgent)) return; - var userAgent = HttpContextCurrent.Request.UserAgent; + var userAgent = CurrentHttpContext.Request.UserAgent; var httpRequestUserAgentProperty = new LogEventProperty(HttpRequestUserAgentPropertyName, new ScalarValue(userAgent)); logEvent.AddPropertyIfAbsent(httpRequestUserAgentProperty); } diff --git a/src/SerilogWeb.Classic/Classic/HttpApplicationWrapper.cs b/src/SerilogWeb.Classic/Classic/HttpApplicationWrapper.cs index d093416..ea61f88 100644 --- a/src/SerilogWeb.Classic/Classic/HttpApplicationWrapper.cs +++ b/src/SerilogWeb.Classic/Classic/HttpApplicationWrapper.cs @@ -26,7 +26,7 @@ public HttpRequestBase Request { get { - var req = HttpContextCurrent.Request; + var req = CurrentHttpContext.Request; if (req == null) return null; return new HttpRequestWrapper(req); } diff --git a/src/SerilogWeb.Classic/SerilogWeb.Classic.csproj b/src/SerilogWeb.Classic/SerilogWeb.Classic.csproj index 7bdcfee..8b9107b 100644 --- a/src/SerilogWeb.Classic/SerilogWeb.Classic.csproj +++ b/src/SerilogWeb.Classic/SerilogWeb.Classic.csproj @@ -59,7 +59,7 @@ - + From 58cde3ac158bb0122a16b86f18c780ef49cc8e4f Mon Sep 17 00:00:00 2001 From: liviriniu Date: Mon, 20 Apr 2020 17:08:35 +0300 Subject: [PATCH 4/4] nitpickery - status code >= 500 consistency in names/comments --- src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs | 2 +- .../Classic/SerilogWebClassicConfigurationBuilder.cs | 4 ++-- .../ModuleConfigurationContractTests.cs | 2 +- .../SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs b/src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs index e6da0b7..09fdc57 100644 --- a/src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs +++ b/src/SerilogWeb.Classic/Classic/ApplicationLifecycleModule.cs @@ -86,7 +86,7 @@ public static Func RequestFilter /// /// When set to Always, form data will be written via an event (using /// severity from FormDataLoggingLevel). When set to OnlyOnError, this - /// will only be written if the Response has a 500 status. + /// will only be written if the Response has status code >= 500. /// When set to OnMatch /// is executed to determine if form data is logged. /// The default is Never. Requires that is also diff --git a/src/SerilogWeb.Classic/Classic/SerilogWebClassicConfigurationBuilder.cs b/src/SerilogWeb.Classic/Classic/SerilogWebClassicConfigurationBuilder.cs index 48e8e07..3dcd52f 100644 --- a/src/SerilogWeb.Classic/Classic/SerilogWebClassicConfigurationBuilder.cs +++ b/src/SerilogWeb.Classic/Classic/SerilogWebClassicConfigurationBuilder.cs @@ -210,7 +210,7 @@ public FormDataLoggingConfigurationBuilder AtLevel(LogEventLevel level) } /// - /// Specify that FormData should be attached to logged events only in case of error (Status > 500) + /// Specify that FormData should be attached to logged events only in case of error (status code >= 500) /// /// A configuration object to allow chaining public FormDataLoggingConfigurationBuilder OnlyOnError() @@ -221,7 +221,7 @@ public FormDataLoggingConfigurationBuilder OnlyOnError() } /// - /// Specify that FormData should be attached to logged events only when the provided condistion is true + /// Specify that FormData should be attached to logged events only when the provided condition is true /// /// The predicate that defines when FormData should be attached /// A configuration object to allow chaining diff --git a/test/SerilogWeb.Classic.Tests/ModuleConfigurationContractTests.cs b/test/SerilogWeb.Classic.Tests/ModuleConfigurationContractTests.cs index f25356d..f53e9a0 100644 --- a/test/SerilogWeb.Classic.Tests/ModuleConfigurationContractTests.cs +++ b/test/SerilogWeb.Classic.Tests/ModuleConfigurationContractTests.cs @@ -396,7 +396,7 @@ public void RequestFiltering() [InlineData(500, true)] [InlineData(501, true)] [InlineData(499, false)] - public void StatusCodeBiggerThan500AreLoggedAsError(int httpStatusCode, bool isLoggedAsError) + public void StatusCodeEqualOrBiggerThan500AreLoggedAsError(int httpStatusCode, bool isLoggedAsError) { TestContext.SimulateRequest(httpStatusCode: httpStatusCode); diff --git a/test/SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs b/test/SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs index f9c907b..a1dfbf2 100644 --- a/test/SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs +++ b/test/SerilogWeb.Classic.Tests/WebRequestLoggingHandlerTests.cs @@ -452,7 +452,7 @@ public void RequestFiltering() [InlineData(500, true)] [InlineData(501, true)] [InlineData(499, false)] - public void StatusCodeBiggerThan500AreLoggedAsError(int httpStatusCode, bool isLoggedAsError) + public void StatusCodeEqualOrBiggerThan500AreLoggedAsError(int httpStatusCode, bool isLoggedAsError) { TestContext.SimulateRequest(httpStatusCode: httpStatusCode);