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 19, 2018
1 parent a6f0282 commit aede302
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 164 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,18 @@ public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
if (this.Variable == null)
if (Variable == null)
{
return;
}
var context = HttpContextAccessor.HttpContext;

if (context.Application == null)
var application = HttpContextAccessor.HttpContext.Application;
if (application == null)
{
return;
}

builder.Append(Convert.ToString(context.Application[this.Variable], CultureInfo.CurrentUICulture));
builder.Append(Convert.ToString(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,13 @@ 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)
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,12 @@ 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)
{
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 +55,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 +66,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 +93,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,13 +25,15 @@ 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)
return;

#if ASP_NET_CORE
var host = request?.Host;
var host = request.Host;
#else
var host = request?.UserHostName;
var host = request.UserHostName;
#endif

if (host != null)
{
var hostString = host.ToString();
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,8 +28,7 @@ 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)
return;

Expand All @@ -41,7 +38,6 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#else
httpMethod = httpRequest.Method;
#endif

builder.Append(httpMethod);

}
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,12 +22,7 @@ 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"];
#else
var ip = httpContext.Connection?.RemoteIpAddress;
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,15 +40,14 @@ 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)
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;

if (queryStrings == null)
return;

Expand All @@ -67,7 +65,6 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
#else
var queryStrings = httpRequest.Query;

if (queryStrings == null)
return;

Expand All @@ -87,8 +84,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#else
NameValueCollection queryStrings,
#endif
IEnumerable<string> queryStringKeys)

List<string> queryStringKeys)
{
if (queryStrings.Count > 0)
{
Expand Down

0 comments on commit aede302

Please sign in to comment.