Skip to content

Commit

Permalink
AspNetQueryStringLayoutRenderer - Reduce code duplication between Asp…
Browse files Browse the repository at this point in the history
…NetCore and AspNetClassic (#769)
  • Loading branch information
snakefoot authored Apr 30, 2022
1 parent 58aef5c commit 54e3920
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, string co
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, env, options);
// Delay initialization of targets until we have loaded config-settings
LogManager.LoadConfiguration(configFileName);
provider.LogFactory.Setup().LoadConfigurationFromFile(configFileName);
return provider;
});
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
using System;
#if !ASP_NET_CORE
using System.Collections.Specialized;
using System.Linq;
#else
using Microsoft.AspNetCore.Http;
#endif
Expand Down Expand Up @@ -78,7 +77,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#if !ASP_NET_CORE
private IEnumerable<KeyValuePair<string, string>> GetHeaderValues(NameValueCollection headers, bool checkForExclude)
{
var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast<string>().ToList();
var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast<string>();
foreach (var headerName in headerNames)
{
if (checkForExclude && Exclude.Contains(headerName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
#if !ASP_NET_CORE
using System.Collections.Specialized;
using System.Web;
#else
using Microsoft.AspNetCore.Http;

#endif

namespace NLog.Web.LayoutRenderers
Expand Down Expand Up @@ -48,17 +44,19 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

var printAllQueryString = QueryStringKeys == null || QueryStringKeys.Count == 0;
var queryStringKeys = QueryStringKeys;
#if !ASP_NET_CORE
var queryStrings = httpRequest.QueryString;
#else
var queryStrings = httpRequest.Query;
#endif
if (queryStrings == null || queryStrings.Count == 0)
return;

var queryStringKeys = QueryStringKeys;
var printAllQueryString = queryStringKeys == null || queryStringKeys.Count == 0;
if (printAllQueryString)
{
queryStringKeys = new List<string>(queryStrings.Keys.Count);

queryStringKeys = new List<string>(queryStrings.Count);
foreach (var key in queryStrings.Keys)
{
if (key != null)
Expand All @@ -67,18 +65,6 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
}
}
#else
var queryStrings = httpRequest.Query;
if (queryStrings == null || queryStrings.Count == 0)
{
return;
}

if (printAllQueryString)
{
queryStringKeys = queryStrings.Keys.ToList();
}
#endif

var pairs = GetPairs(queryStrings, queryStringKeys);
SerializePairs(pairs, builder, logEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#else
RouteValueDictionary routeParameters = context.GetRouteData()?.Values;
#endif

bool printAllRouteParameter = RouteParameterKeys == null || RouteParameterKeys.Count == 0;
List<string> routeParameterKeys = RouteParameterKeys;
if (routeParameters == null || routeParameters.Count == 0)
{
return;
}

var routeParameterKeys = RouteParameterKeys;
bool printAllRouteParameter = routeParameterKeys == null || routeParameterKeys.Count == 0;
if (printAllRouteParameter)
{
routeParameterKeys = routeParameters.Keys.ToList();
Expand All @@ -70,13 +69,12 @@ private static IEnumerable<KeyValuePair<string, string>> GetPairs(RouteValueDict
foreach (string key in routeParameterKeys)
{
// This platform specific code is to prevent an unncessary .ToString call otherwise.

if (!routeParameters.TryGetValue(key, out object objValue))
{
continue;
}

string value = objValue.ToString();
string value = objValue?.ToString();
if (!string.IsNullOrEmpty(value))
{
yield return new KeyValuePair<string, string>(key, value);
Expand Down

0 comments on commit 54e3920

Please sign in to comment.