Skip to content

Commit

Permalink
Skip null-check of HttpContextAccessor.HttpContext in DoAppend (Has b…
Browse files Browse the repository at this point in the history
…een checked)
  • Loading branch information
snakefoot committed Sep 18, 2018
1 parent a6f0282 commit 885c38b
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AspNetCookieLayoutRendererTests() : base()
public void NullKeyRendersEmptyString()
{
#if ASP_NET_CORE
var httpContext = this.HttpContext;
var httpContext = HttpContext;
#else
var httpContext = Substitute.For<HttpContextBase>();
#endif
Expand Down Expand Up @@ -322,7 +322,7 @@ public void CommaSeperatedCookieNamesTest_Multiple_Cookie_Values_Flat_Formatting
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";

this.HttpContext.Request.Cookies.Add(cookie);
HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetRequestCookieLayoutRenderer;

Expand All @@ -347,7 +347,7 @@ public void CommaSeperatedCookieNamesTest_Multiple_Cookie_Values_Json_Formatting
var cookie = new HttpCookie("key", "TEST");
cookie["Key1"] = "TEST1";

this.HttpContext.Request.Cookies.Add(cookie);
HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetRequestCookieLayoutRenderer;

Expand Down Expand Up @@ -376,7 +376,7 @@ private AspNetRequestCookieLayoutRenderer CreateRenderer(bool addSecondCookie =
{
var cookieNames = new List<string>();
#if ASP_NET_CORE
var httpContext = this.HttpContext;
var httpContext = HttpContext;
#else
var httpContext = Substitute.For<HttpContextBase>();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private AspNetQueryStringLayoutRenderer CreateAndMockRenderer(params Tuple<strin
var httpContext = Substitute.For<HttpContextBase>();
var pairCollection = new NameValueCollection();
#else
var httpContext = this.HttpContext;
var httpContext = HttpContext;
var pairCollection = new QueryBuilder();
#endif

Expand Down
5 changes: 3 additions & 2 deletions NLog.Web.AspNetCore/DefaultHttpContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public HttpContextBase HttpContext
{
get
{
if (System.Web.HttpContext.Current == null)
var httpContext = System.Web.HttpContext.Current;
if (httpContext == null)
return null;
return new HttpContextWrapper(System.Web.HttpContext.Current);
return new HttpContextWrapper(httpContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
Expand Down Expand Up @@ -36,7 +37,6 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-application")]
public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
{

/// <summary>
/// Gets or sets the variable name.
/// </summary>
Expand All @@ -52,18 +52,13 @@ public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
if (this.Variable == null)
{
return;
}
var context = HttpContextAccessor.HttpContext;

if (context.Application == null)
if (Variable == null)
{
return;
}

builder.Append(Convert.ToString(context.Application[this.Variable], CultureInfo.CurrentUICulture));
var context = HttpContextAccessor.HttpContext;
builder.Append(Convert.ToString(context.Application[Variable], CultureInfo.CurrentUICulture));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
/// </summary>
public AspNetItemValueLayoutRenderer()
{
this.Culture = CultureInfo.CurrentUICulture;
Culture = CultureInfo.CurrentUICulture;
}

/// <summary>
Expand Down
16 changes: 11 additions & 5 deletions NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NLog.LayoutRenderers;

#if ASP_NET_CORE
using NLog.Web.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using NLog.Web.DependencyInjection;
#endif

using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
Expand Down Expand Up @@ -89,9 +88,16 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor()
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (HttpContextAccessor?.HttpContext == null)
var httpContextAccessor = HttpContextAccessor;
if (httpContextAccessor == null)
return;

if (httpContextAccessor.HttpContext == null)
{
Common.InternalLogger.Debug("No available HttpContext. Logging outside valid request context?");
return;
}

DoAppend(builder, logEvent);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#if ASP_NET_CORE
using NLog.LayoutRenderers;
using System.Text;

using Microsoft.AspNetCore.Routing;

using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -26,13 +23,16 @@ public class AspNetRequestContentTypeLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var request = HttpContextAccessor?.HttpContext?.TryGetRequest();

var contentType = request?.ContentType;
var request = HttpContextAccessor.HttpContext.TryGetRequest();
if (request == null)
{
Common.InternalLogger.Debug("aspnet-request-contenttype - HttpContext Request is null");
return;
}

var contentType = request.ContentType;
if (!string.IsNullOrEmpty(contentType))
builder.Append(contentType);

builder.Append(contentType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
#if !ASP_NET_CORE
using System.Collections.Specialized;
using System.Web;
#else
using Microsoft.Extensions.Primitives;
using Microsoft.AspNetCore.Http;
#endif
using NLog.LayoutRenderers;
using System.Collections.Generic;
using NLog.Config;
using NLog.Web.Enums;
using System;
using System.Linq;

using NLog.Web.Internal;

Expand Down Expand Up @@ -42,16 +39,16 @@ public class AspNetRequestCookieLayoutRenderer : AspNetLayoutMultiValueRendererB
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();

var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
Common.InternalLogger.Debug("aspnet-request-cookie - HttpContext Request is null");
return;
}

var cookies = httpRequest.Cookies;

if (this.CookieNames?.Count > 0 && cookies?.Count > 0)
if (CookieNames?.Count > 0 && cookies?.Count > 0)
{
var cookieValues = GetCookies(cookies);
SerializePairs(cookieValues, builder);
Expand All @@ -62,7 +59,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

private IEnumerable<KeyValuePair<string, string>> GetCookies(HttpCookieCollection cookies)
{
var cookieNames = this.CookieNames;
var cookieNames = CookieNames;
if (cookieNames != null)
{
foreach (var cookieName in cookieNames)
Expand All @@ -73,7 +70,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
continue;
}

if (this.OutputFormat == AspNetRequestLayoutOutputFormat.Json)
if (OutputFormat == AspNetRequestLayoutOutputFormat.Json)
{
// Split multi-valued cookie, as allowed for in the HttpCookie API for backwards compatibility with classic ASP
var isFirst = true;
Expand All @@ -100,7 +97,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

private IEnumerable<KeyValuePair<string, string>> GetCookies(IRequestCookieCollection cookies)
{
var cookieNames = this.CookieNames;
var cookieNames = CookieNames;
if (cookieNames != null)
{
foreach (var cookieName in cookieNames)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

using NLog.LayoutRenderers;
using System.Text;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Routing;
#endif
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -29,7 +25,13 @@ public class AspNetRequestHostLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var request = HttpContextAccessor?.HttpContext?.TryGetRequest();
var request = HttpContextAccessor.HttpContext.TryGetRequest();
if (request == null)
{
Common.InternalLogger.Debug("aspnet-request-host - HttpContext Request is null");
return;
}

#if ASP_NET_CORE
var host = request?.Host;
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Text;
using System;
using System.Text;
#if !ASP_NET_CORE
using System.Web;
using System.Collections.Specialized;
#endif
using NLog.LayoutRenderers;
using System.Collections.Generic;
using NLog.Config;
using System;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -30,10 +28,12 @@ public class AspNetRequestHttpMethodRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();

var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
Common.InternalLogger.Debug("aspnet-request-method - HttpContext Request is null");
return;
}

string httpMethod;
#if !ASP_NET_CORE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Text;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

Expand All @@ -26,10 +22,6 @@ public class AspNetRequestIpLayoutRenderer : AspNetLayoutRendererBase
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpContext = HttpContextAccessor.HttpContext;
if (httpContext == null)
{
return;
}
#if !ASP_NET_CORE

var ip = httpContext.TryGetRequest()?.ServerVariables["REMOTE_ADDR"];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !ASP_NET_CORE
using System.Web;
using System.Collections.Specialized;
using System.Web;
#else
using Microsoft.AspNetCore.Http;
#endif
using NLog.LayoutRenderers;
using System.Collections.Generic;
using NLog.Config;
using NLog.Web.Enums;
using System;
using System.Linq;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand Down Expand Up @@ -41,12 +40,15 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutMultiValueRendererBas
/// <param name="logEvent"></param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
Common.InternalLogger.Debug("aspnet-request-querystring - HttpContext Request is null");
return;
}

var printAllQueryString = this.QueryStringKeys == null || this.QueryStringKeys.Count == 0;
var queryStringKeys = this.QueryStringKeys;
var printAllQueryString = QueryStringKeys == null || QueryStringKeys.Count == 0;
var queryStringKeys = QueryStringKeys;
#if !ASP_NET_CORE
var queryStrings = httpRequest.QueryString;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Text;
using System;
using System.Text;
#if !ASP_NET_CORE
using System.Web;
using System.Collections.Specialized;
#endif
using NLog.LayoutRenderers;
using System.Collections.Generic;
using NLog.Config;
using System;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -30,10 +28,12 @@ public class AspNetRequestReferrerRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();

var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
Common.InternalLogger.Debug("aspnet-request-referrer - HttpContext Request is null");
return;
}

string referrer;

Expand Down

0 comments on commit 885c38b

Please sign in to comment.