diff --git a/NLog.Web.AspNetCore/Internal/PropertyReader.cs b/NLog.Web.AspNetCore/Internal/PropertyReader.cs index f3521980..a7f2caef 100644 --- a/NLog.Web.AspNetCore/Internal/PropertyReader.cs +++ b/NLog.Web.AspNetCore/Internal/PropertyReader.cs @@ -21,31 +21,29 @@ public static object GetValue(string key, Func getVal, bool eval return null; } - object value; - if (evaluateAsNestedProperties) - { - var path = key.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries); + var value = evaluateAsNestedProperties ? GetValueAsNestedProperties(key, getVal) : getVal(key); + return value; + } + + private static object GetValueAsNestedProperties(string key, Func getVal) + { + var path = key.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries); - value = getVal(path.First()); + var value = getVal(path.First()); - if (value != null) + if (value != null) + { + foreach (var property in path.Skip(1)) { - foreach (var property in path.Skip(1)) + var propertyInfo = GetPropertyInfo(value, property); + value = propertyInfo?.GetValue(value, null); + if (value == null) { - var propertyInfo = GetPropertyInfo(value, property); - value = propertyInfo?.GetValue(value, null); - if (value == null) - { - //done - break; - } + //done + break; } } } - else - { - value = getVal(key); - } return value; } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs index 94cb3d6c..61bd6fe8 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs @@ -42,67 +42,75 @@ public abstract class AspNetLayoutMultiValueRendererBase : AspNetLayoutRendererB protected void SerializeValues(IEnumerable> values, StringBuilder builder) { - var firstItem = true; + switch (OutputFormat) { case AspNetRequestLayoutOutputFormat.Flat: - - foreach (var kpv in values) - { - var key = kpv.Key; - var value = kpv.Value; - - if (!firstItem) - { - builder.Append(ItemSeparator); - } - firstItem = false; - builder.Append(key); - builder.Append(ValueSeparator); - builder.Append(value); - } - - + SerializValuesFlat(values, builder); break; case AspNetRequestLayoutOutputFormat.Json: + SerializeValuesJson(values, builder); + break; + } + } + private void SerializeValuesJson(IEnumerable> values, StringBuilder builder) + { + var firstItem = true; + var valueList = values.ToList(); - var valueList = values.ToList(); - - if (valueList.Count > 0) + if (valueList.Count > 0) + { + var addArray = valueList.Count > (SingleAsArray ? 0 : 1); + + if (addArray) + { + builder.Append('['); + } + + foreach (var kpv in valueList) + { + var key = kpv.Key; + var value = kpv.Value; + if (!firstItem) { - var addArray = valueList.Count > (SingleAsArray ? 0 : 1); - - if (addArray) - { - builder.Append('['); - } - - foreach (var kpv in valueList) - { - var key = kpv.Key; - var value = kpv.Value; - if (!firstItem) - { - builder.Append(','); - } - firstItem = false; - - //quoted key - builder.Append('{'); - AppendQuoted(builder, key); - - builder.Append(':'); - //quoted value; - AppendQuoted(builder, value); - builder.Append('}'); - } - if (addArray) - { - builder.Append(']'); - } + builder.Append(','); } - break; + firstItem = false; + + //quoted key + builder.Append('{'); + AppendQuoted(builder, key); + + builder.Append(':'); + //quoted value; + AppendQuoted(builder, value); + builder.Append('}'); + } + if (addArray) + { + builder.Append(']'); + } + } + + } + + private void SerializValuesFlat(IEnumerable> values, StringBuilder builder) + { + var firstItem = true; + foreach (var kpv in values) + { + var key = kpv.Key; + var value = kpv.Value; + + if (!firstItem) + { + builder.Append(ItemSeparator); + } + firstItem = false; + builder.Append(key); + builder.Append(ValueSeparator); + builder.Append(value); } } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcActionRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcActionRenderer.cs index ef00fe2c..5cf35fbf 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcActionRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcActionRenderer.cs @@ -34,12 +34,11 @@ public class AspNetMvcActionRenderer : AspNetMvcLayoutRendererBase protected override void MvcDoAppend(StringBuilder builder, LogEventInfo logEvent, HttpContextBase context) { string key = "action"; - string controller; #if !ASP_NET_CORE - controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString(); + var controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString(); #else - controller = context?.GetRouteData()?.Values?[key]?.ToString(); + var controller = context?.GetRouteData()?.Values?[key]?.ToString(); #endif if (!string.IsNullOrEmpty(controller)) builder.Append(controller); diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcControllerRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcControllerRenderer.cs index 2298cb97..b491b8e2 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcControllerRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetMvcControllerRenderer.cs @@ -35,12 +35,11 @@ public class AspNetMvcControllerRenderer : AspNetMvcLayoutRendererBase protected override void MvcDoAppend(StringBuilder builder, LogEventInfo logEvent, HttpContextBase context) { string key = "controller"; - string controller; #if !ASP_NET_CORE - controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString(); + var controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString(); #else - controller = context?.GetRouteData()?.Values?[key]?.ToString(); + var controller = context?.GetRouteData()?.Values?[key]?.ToString(); #endif if (!string.IsNullOrEmpty(controller)) builder.Append(controller); diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs index 49d9fb7c..f5118234 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs @@ -63,15 +63,12 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) private IEnumerable> GetCookies(HttpCookieCollection cookies) { - var cookieNames = this.CookieNames; - if (cookieNames != null) + if (CookieNames != null) { - foreach (var cookieName in cookieNames) + foreach (var cookieName in CookieNames) { var value = cookies[cookieName]; - - - + if (value != null) { if (this.OutputFormat == AspNetRequestLayoutOutputFormat.Json) diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestUrlRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestUrlRenderer.cs index 957f7af2..2e9afe84 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestUrlRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestUrlRenderer.cs @@ -62,14 +62,20 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) if (httpRequest == null) return; - string url, pathAndQuery, port, host, scheme; - url = pathAndQuery = port = host = scheme = null; + var url = CreateUrl(httpRequest); + + builder.Append(url); + } #if !ASP_NET_CORE - + + private string CreateUrl(HttpRequestBase httpRequest) + { + string port = null, host = null, scheme = null; + if (httpRequest.Url == null) { - return; + return null; } if (IncludePort && httpRequest.Url.Port > 0) @@ -77,14 +83,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) port = ":" + httpRequest.Url.Port; } - if (IncludeQueryString) - { - pathAndQuery = httpRequest.Url.PathAndQuery; - } - else - { - pathAndQuery = httpRequest.Url.AbsolutePath; - } + var pathAndQuery = IncludeQueryString ? httpRequest.Url.PathAndQuery : httpRequest.Url.AbsolutePath; if (IncludeHost) { @@ -96,8 +95,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) scheme = httpRequest.Url.Scheme + "://"; } - url = $"{scheme}{host}{port}{pathAndQuery}"; + var url = $"{scheme}{host}{port}{pathAndQuery}"; + return url; + } + #else + private string CreateUrl(Microsoft.AspNetCore.Http.HttpRequest httpRequest) + { + string pathAndQuery = null, port = null, host = null, scheme = null; + if (IncludeQueryString) { pathAndQuery = httpRequest.QueryString.Value; @@ -114,13 +120,14 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) } if (IncludeScheme && !String.IsNullOrWhiteSpace(httpRequest.Scheme)) - { + { scheme = httpRequest.Scheme + "://"; } - url = $"{scheme}{host}{port}{httpRequest.PathBase.ToUriComponent()}{httpRequest.Path.ToUriComponent()}{pathAndQuery}"; -#endif - builder.Append(url); + var url = $"{scheme}{host}{port}{httpRequest.PathBase.ToUriComponent()}{httpRequest.Path.ToUriComponent()}{pathAndQuery}"; + return url; } + +#endif } } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index 6e75fa40..73e24dd9 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -89,62 +89,85 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) return; } - if (this.QueryString != null) + if (QueryString != null) { -#if !ASP_NET_CORE - if (httpRequest.QueryString != null) - { - builder.Append(httpRequest.QueryString[this.QueryString]); - } -#else - if (httpRequest.Query != null) - { - builder.Append(httpRequest.Query[this.QueryString]); - } -#endif + AppendQueryString(builder, httpRequest); } - else if (this.Form != null && httpRequest.Form != null) + else if (Form != null && httpRequest.Form != null) { - builder.Append(httpRequest.Form[this.Form]); + builder.Append(httpRequest.Form[Form]); } - else if (this.Cookie != null && httpRequest.Cookies != null) + else if (Cookie != null && httpRequest.Cookies != null) { -#if !ASP_NET_CORE - var cookie = httpRequest.Cookies[this.Cookie]; - - if (cookie != null) - { - builder.Append(cookie.Value); - } -#else - var cookie = httpRequest.Cookies[this.Cookie]; - builder.Append(cookie); -#endif - + AppendCookie(builder, httpRequest); } #if !ASP_NET_CORE - else if (this.ServerVariable != null && httpRequest.ServerVariables != null) + else if (ServerVariable != null && httpRequest.ServerVariables != null) { - builder.Append(httpRequest.ServerVariables[this.ServerVariable]); + builder.Append(httpRequest.ServerVariables[ServerVariable]); } #endif - else if (this.Header != null && httpRequest.Headers != null) + else if (Header != null && httpRequest.Headers != null) { - string header = httpRequest.Headers[this.Header]; + string header = httpRequest.Headers[Header]; if (header != null) { builder.Append(header); } } - else if (this.Item != null) + else if (Item != null) { + AppendItem(builder, httpRequest); + } + } + + #if !ASP_NET_CORE - builder.Append(httpRequest[this.Item]); + + + private void AppendQueryString(StringBuilder builder, HttpRequestBase httpRequest) + { + if (httpRequest.QueryString != null) + { + builder.Append(httpRequest.QueryString[QueryString]); + } + } + + private void AppendCookie(StringBuilder builder, HttpRequestBase httpRequest) + { + var cookie = httpRequest.Cookies[Cookie]; + + if (cookie != null) + { + builder.Append(cookie.Value); + } + } + + private void AppendItem(StringBuilder builder, HttpRequestBase httpRequest) + { + builder.Append(httpRequest[Item]); + } #else - builder.Append(httpRequest.HttpContext.Items[this.Item]); -#endif + + private void AppendQueryString(StringBuilder builder, HttpRequest httpRequest) + { + if (httpRequest.Query != null) + { + builder.Append(httpRequest.Query[this.QueryString]); } } + + private void AppendCookie(StringBuilder builder, HttpRequest httpRequest) + { + var cookie = httpRequest.Cookies[Cookie]; + builder.Append(cookie); + } + + private void AppendItem(StringBuilder builder, HttpRequest httpRequest) + { + builder.Append(httpRequest.HttpContext.Items[this.Item]); + } +#endif } } \ No newline at end of file diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 6a3f913f..c8c62485 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -78,10 +78,7 @@ public AspNetSessionValueLayoutRenderer() /// Logging event. protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - if (this.Variable == null) - { - return; - } + if (this.Variable == null) { return; } var context = HttpContextAccessor.HttpContext; if (context?.Session == null) @@ -92,23 +89,11 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #if !ASP_NET_CORE var value = PropertyReader.GetValue(Variable, k => context.Session[k], EvaluateAsNestedProperties); #else - if (context.Items == null) - { - return; - } - - if (context.Features.Get()?.Session == null) - { - return; - } + if (context.Items == null || context.Features.Get()?.Session == null) { return; } //because session.get / session.getstring also creating log messages in some cases, this could lead to stackoverflow issues. //We remember on the context.Items that we are looking up a session value so we prevent stackoverflows - if (context.Items.ContainsKey(NLogRetrievingSessionValue)) - { - //prevent stackoverflow - return; - } + if (context.Items.ContainsKey(NLogRetrievingSessionValue)) { return; } context.Items[NLogRetrievingSessionValue] = true; object value; @@ -126,13 +111,13 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) context.Items.Remove(NLogRetrievingSessionValue); } - - #endif var formatProvider = GetFormatProvider(logEvent, Culture); builder.Append(Convert.ToString(value, formatProvider)); } +#if ASP_NET_CORE private const string NLogRetrievingSessionValue = "NLogRetrievingSessionValue"; +#endif } } \ No newline at end of file diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs index 9f3b6af9..436ae9f9 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AssemblyVersionLayoutRenderer.cs @@ -59,39 +59,43 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent) } else { - //try entry assembly - -#if ASP_NET_CORE && !NETSTANDARD2_0 - string assemblyVersion = PlatformServices.Default.Application.RuntimeFramework.Version.ToString(); + string assemblyVersion = GetAssemblyVersion(); + builder.Append(assemblyVersion ?? "Could not entry assembly"); + } + } - builder.Append(assemblyVersion); +#if ASP_NET_CORE && !NETSTANDARD2_0 + private static string GetAssemblyVersion() + { + return PlatformServices.Default.Application.RuntimeFramework.Version.ToString(); + } #elif NETSTANDARD2_0 - var assemblyVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(); - builder.Append(assemblyVersion); + private static string GetAssemblyVersion() + { + return System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(); + } #else + private static string GetAssemblyVersion() + { - var assembly = Assembly.GetEntryAssembly(); - - if (assembly == null) - { - assembly = GetAspNetEntryAssembly(); - } - if (assembly == null) - { - builder.Append("Could not entry assembly"); - } - else - { - builder.Append(assembly.GetName().Version.ToString()); - } -#endif - + var assembly = Assembly.GetEntryAssembly(); + if (assembly == null) + { + assembly = GetAspNetEntryAssembly(); + } + if (assembly == null) + { + return null; + } + else + { + var version = assembly.GetName().Version.ToString(); + return version; } - } -#if !ASP_NET_CORE + private static Assembly GetAspNetEntryAssembly() { if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null)