From 900686c21b9d09023dc853a03608687ad5728e70 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sat, 24 Dec 2016 01:57:04 +0100 Subject: [PATCH] added null checks --- NLog.Web.AspNetCore/Internal/PropertyReader.cs | 18 +++++++++++++----- .../AspNetItemValueLayoutRenderer.cs | 5 +++++ .../AspNetRequestValueLayoutRenderer.cs | 2 +- .../AspNetSessionIDLayoutRenderer.cs | 2 +- .../AspNetSessionValueLayoutRenderer.cs | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/NLog.Web.AspNetCore/Internal/PropertyReader.cs b/NLog.Web.AspNetCore/Internal/PropertyReader.cs index 4361f3ae..dcd35a67 100644 --- a/NLog.Web.AspNetCore/Internal/PropertyReader.cs +++ b/NLog.Web.AspNetCore/Internal/PropertyReader.cs @@ -28,10 +28,18 @@ public static object GetValue(string key, Func getVal, bool eval value = getVal(path.First()); - foreach (var property in path.Skip(1)) + if (value != null) { - var propertyInfo = GetPropertyInfo(value, property); - value = propertyInfo?.GetValue(value, null); + foreach (var property in path.Skip(1)) + { + var propertyInfo = GetPropertyInfo(value, property); + value = propertyInfo?.GetValue(value, null); + if (value == null) + { + //done + break; + } + } } } else @@ -44,9 +52,9 @@ public static object GetValue(string key, Func getVal, bool eval private static PropertyInfo GetPropertyInfo(object value, string propertyName) { #if !NETSTANDARD_1plus - return value.GetType().GetProperty(propertyName); + return value?.GetType().GetProperty(propertyName); #else - return value.GetType().GetTypeInfo().GetDeclaredProperty(propertyName); + return value?.GetType().GetTypeInfo().GetDeclaredProperty(propertyName); #endif } } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs index e41cd25c..3eea0e14 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs @@ -68,6 +68,11 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) } var context = HttpContextAccessor.HttpContext; + if (context == null) + { + return; + } + Func getVal = k => context.Items[k]; var value = PropertyReader.GetValue(Variable, getVal, EvaluateAsNestedProperties); diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index 55eed2c4..2ee886a0 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -83,7 +83,7 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase /// Logging event. protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest(); + var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest(); if (httpRequest == null) { return; diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs index 94dfb3b5..94271e52 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs @@ -23,7 +23,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { var context = HttpContextAccessor.HttpContext; - if (context.Session == null) + if (context?.Session == null) { return; } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 98dd32a2..958eb514 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -71,7 +71,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) } var context = HttpContextAccessor.HttpContext; - if (context.Session == null) + if (context?.Session == null) { return; }