Skip to content

Commit

Permalink
support for empty 'QueryStringKeys' for ${all-querystrings{aspnet-req…
Browse files Browse the repository at this point in the history
…uest-querystring}
  • Loading branch information
304NotModified committed Feb 10, 2017
1 parent 324e24d commit 6b68413
Showing 1 changed file with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NLog.Config;
using NLog.Web.Enums;
using System;
using System.Linq;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -29,6 +30,7 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// List Query Strings' Key to be rendered from Request.
/// If empty, then render all querystrings
/// </summary>
public List<String> QueryStringKeys { get; set; }

Expand All @@ -46,41 +48,60 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutRendererBase
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();
if (httpRequest == null)
return;

if (this.QueryStringKeys?.Count > 0)
{
if (httpRequest == null)
return;

var includeArrayEndBraces = false;
var firstItem = true;
var includeArrayEndBraces = false;
var firstItem = true;


var allQueryStrings = this.QueryStringKeys == null || this.QueryStringKeys.Count == 0;
var queryStringKeys = this.QueryStringKeys;
#if !NETSTANDARD_1plus
var queryStrings = httpRequest.QueryString;
var queryStrings = httpRequest.QueryString;
if (allQueryStrings)
{
queryStringKeys = new List<string>(queryStrings.Keys.Count);

foreach (var key in queryStrings.Keys)
{
if (key != null)
{
queryStringKeys.Add(key.ToString());
}
}
}
#else
var queryStrings = httpRequest.Query;
var queryStrings = httpRequest.Query;
if (allQueryStrings)
{
queryStringKeys = queryStrings.Keys.ToList();
}
#endif
if (queryStrings?.Count > 0)




if (queryStrings?.Count > 0)
{
foreach (var configuredKey in queryStringKeys)
{
foreach (var configuredKey in this.QueryStringKeys)
{
// This platoform specific code is to prevent an unncessary .ToString call otherwise.
// This platoform specific code is to prevent an unncessary .ToString call otherwise.
#if !NETSTANDARD_1plus
var value = queryStrings[configuredKey];
var value = queryStrings[configuredKey];
#else
var value = queryStrings[configuredKey].ToString();
var value = queryStrings[configuredKey].ToString();
#endif
if (!String.IsNullOrEmpty(value))
{
this.AppendKeyAndValue(builder, configuredKey, value, firstItem, ref includeArrayEndBraces);
firstItem = false;
}
if (!String.IsNullOrEmpty(value))
{
this.AppendKeyAndValue(builder, configuredKey, value, firstItem, ref includeArrayEndBraces);
firstItem = false;
}
}

if (includeArrayEndBraces)
builder.Append(GlobalConstants.jsonArrayEndBraces);
}

if (includeArrayEndBraces)
builder.Append(GlobalConstants.jsonArrayEndBraces);
}

/// <summary>
Expand Down

0 comments on commit 6b68413

Please sign in to comment.