Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #68 from liviriniu/master
Browse files Browse the repository at this point in the history
  • Loading branch information
tsimbalar authored Apr 21, 2020
2 parents 48929ab + 58cde3a commit 52b8b5a
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Func<HttpContextBase, bool> RequestFilter
/// <summary>
/// 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 <see cref="ShouldLogPostedFormData"/>
/// is executed to determine if form data is logged.
/// The default is Never. Requires that <see cref="IsEnabled"/> is also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
static class HttpContextCurrent
static class CurrentHttpContext
{
/// <summary>
/// Gets the <see cref="T:System.Web.HttpRequest"/> object for the current HTTP request.
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ 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;

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

using System;
using System.Web;
using Serilog.Core;
using Serilog.Events;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SerilogWeb.Classic/Classic/HttpApplicationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public FormDataLoggingConfigurationBuilder AtLevel(LogEventLevel level)
}

/// <summary>
/// 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)
/// </summary>
/// <returns>A configuration object to allow chaining</returns>
public FormDataLoggingConfigurationBuilder OnlyOnError()
Expand All @@ -221,7 +221,7 @@ public FormDataLoggingConfigurationBuilder OnlyOnError()
}

/// <summary>
/// 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
/// </summary>
/// <param name="matchingFunction">The predicate that defines when FormData should be attached</param>
/// <returns>A configuration object to allow chaining</returns>
Expand Down
2 changes: 1 addition & 1 deletion src/SerilogWeb.Classic/SerilogWeb.Classic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<Compile Include="SerilogWebClassicLoggerConfigurationExtensions.cs" />
<Compile Include="Classic\WebRequestLoggingHandler.cs" />
<Compile Include="Classic\Enrichers\ClaimValueEnricher.cs" />
<Compile Include="Classic\Enrichers\HttpContextCurrent.cs" />
<Compile Include="Classic\Enrichers\CurrentHttpContext.cs" />
<Compile Include="Classic\Enrichers\HttpRequestClientHostIPEnricher.cs" />
<Compile Include="Classic\Enrichers\HttpRequestClientHostNameEnricher.cs" />
<Compile Include="Classic\Enrichers\HttpRequestIdEnricher.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 52b8b5a

Please sign in to comment.