diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index c48e272b..539d0df1 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -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; diff --git a/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs index fbb7ff3c..1ca27025 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs @@ -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 @@ -78,7 +77,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #if !ASP_NET_CORE private IEnumerable> GetHeaderValues(NameValueCollection headers, bool checkForExclude) { - var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast().ToList(); + var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast(); foreach (var headerName in headerNames) { if (checkForExclude && Exclude.Contains(headerName)) diff --git a/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs index f054230d..e381acc7 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs @@ -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 @@ -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(queryStrings.Keys.Count); - + queryStringKeys = new List(queryStrings.Count); foreach (var key in queryStrings.Keys) { if (key != null) @@ -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); diff --git a/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs index e19a4198..233c32b5 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs @@ -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 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(); @@ -70,13 +69,12 @@ private static IEnumerable> 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(key, value);