Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using NLog.Config;
Expand Down Expand Up @@ -43,6 +44,12 @@ public class AspNetSessionValueLayoutRenderer : LayoutRenderer
[DefaultParameter]
public string Variable { get; set; }

/// <summary>
/// Gets or sets whether variables with a dot are evaluated as properties or not
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public bool EvaluateAsNestedProperties { get; set; }

/// <summary>
/// Renders the specified ASP.NET Session value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand All @@ -66,7 +73,26 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
return;
}

builder.Append(Convert.ToString(context.Session[this.Variable], CultureInfo.InvariantCulture));

object value;
if (EvaluateAsNestedProperties)
{
var path = Variable.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

value = context.Session[path[0]];

foreach (var property in path.Skip(1))
{
var propertyInfo = value.GetType().GetProperty(property);
value = propertyInfo.GetValue(value, null);
}
}
else
{
value = context.Session[Variable];
}

builder.Append(Convert.ToString(value, CultureInfo.InvariantCulture));
}
}
}