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/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 83% rename from src/SerilogWeb.Classic/Classic/Enrichers/HttpContextCurrent.cs rename to src/SerilogWeb.Classic/Classic/Enrichers/CurrentHttpContext.cs index 7f3806c..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. @@ -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) { 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 c75e032..d550810 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,10 +40,10 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { if (logEvent == null) throw new ArgumentNullException("logEvent"); - if (HttpContext.Current?.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/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/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 @@ - + 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);