Skip to content

Commit

Permalink
Merge pull request #78 from NLog/check-for-null-session
Browse files Browse the repository at this point in the history
added null checks
  • Loading branch information
304NotModified authored Dec 24, 2016
2 parents abf7e1b + 900686c commit a6f0498
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
18 changes: 13 additions & 5 deletions NLog.Web.AspNetCore/Internal/PropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ public static object GetValue(string key, Func<string, object> 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
Expand All @@ -44,9 +52,9 @@ public static object GetValue(string key, Func<string, object> 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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
var context = HttpContextAccessor.HttpContext;

if (context == null)
{
return;
}

Func<string, object> getVal = k => context.Items[k];

var value = PropertyReader.GetValue(Variable, getVal, EvaluateAsNestedProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();
if (httpRequest == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a6f0498

Please sign in to comment.