Skip to content

Commit

Permalink
Merge pull request #15 from NLog/aspnet-item-renderer
Browse files Browse the repository at this point in the history
Aspnet-item renderer
  • Loading branch information
304NotModified committed Nov 24, 2015
2 parents 9d19558 + 18cd56c commit ce2e477
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,90 @@ public void NestedProps()
ExecTest("a", o, "c", appSettingLayoutRenderer);
}

[TestMethod()]
public void NestedProps2()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = "a.b.c",
EvaluateAsNestedProperties = true
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

[TestMethod()]
public void NestedProps3()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = "a.b..c",
EvaluateAsNestedProperties = true
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

[TestMethod()]
public void EmptyPath()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = "",
EvaluateAsNestedProperties = true
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

[TestMethod()]
public void EmptyVarname()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = "",
EvaluateAsNestedProperties = false
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

[TestMethod()]
public void NullPath()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = null,
EvaluateAsNestedProperties = true
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

[TestMethod()]
public void NullVarname()
{
var appSettingLayoutRenderer = new AspNetSessionValueLayoutRenderer()
{
Variable = null,
EvaluateAsNestedProperties = false
};

var o = new { b = "c" };
//set in "a"
ExecTest("a", o, "", appSettingLayoutRenderer);
}

/// <summary>
/// set in Session and test
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions NLog.Web/Internal/PropertyReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace NLog.Web.Internal
{
internal class PropertyReader
{
/// <summary>
/// Get value of a property
/// </summary>
/// <param name="key">key</param>
/// <param name="getVal">function to get a value with this key</param>
/// <param name="evaluateAsNestedProperties">evaluate <paramref name="key"/> as a nested property path. E.g. A.B is property B inside A.</param>
/// <returns>value</returns>
public static object GetValue(string key, Func<string, object> getVal, bool evaluateAsNestedProperties)
{
object value;
if (evaluateAsNestedProperties)
{
var path = key.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

value = getVal(path.First());

foreach (var property in path.Skip(1))
{
var propertyInfo = value.GetType().GetProperty(property);
value = propertyInfo.GetValue(value, null);
}
}
else
{
value = getVal(key);
}
return value;
}
}
}
77 changes: 77 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET Item variable.
/// </summary>
/// <remarks>
/// Use this layout renderer to insert the value of the specified variable stored
/// in the ASP.NET HttpContext.Current.Items dictionary.
/// </remarks>
/// <example>
/// <para>You can set the value of an ASP.NET Item variable by using the following code:</para>
/// <code lang="C#">
/// <![CDATA[
/// HttpContext.Current.Items["myvariable"] = 123;
/// HttpContext.Current.Items["stringvariable"] = "aaa BBB";
/// HttpContext.Current.Items["anothervariable"] = DateTime.Now;
/// ]]>
/// </code>
/// <para>Example usage of ${aspnet-item}:</para>
/// <code lang="NLog Layout Renderer">
/// ${aspnet-item:variable=myvariable} - produces "123"
/// ${aspnet-item:variable=anothervariable} - produces "01/01/2006 00:00:00"
/// ${aspnet-item:variable=anothervariable:culture=pl-PL} - produces "2006-01-01 00:00:00"
/// ${aspnet-item:variable=myvariable:padding=5} - produces " 123"
/// ${aspnet-item:variable=myvariable:padding=-5} - produces "123 "
/// ${aspnet-item:variable=stringvariable:upperCase=true} - produces "AAA BBB"
/// </code>
/// </example>
[LayoutRenderer("aspnet-item")]
public class AspNetItemValueLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Gets or sets the item variable name.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[DefaultParameter]
public string Variable { get; set; }

/// <summary>
/// Gets or sets whether items 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 Item value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if (Variable == null)
{
return;
}

HttpContext context = HttpContext.Current;
if (context == null)
{
return;
}

var value = PropertyReader.GetValue(Variable, k => context.Items[k], EvaluateAsNestedProperties);

builder.Append(Convert.ToString(value, CultureInfo.InvariantCulture));
}
}
}
22 changes: 3 additions & 19 deletions NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
Expand Down Expand Up @@ -72,25 +74,7 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
return;
}


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];
}
var value = PropertyReader.GetValue(Variable, k => context.Session[k], EvaluateAsNestedProperties);

builder.Append(Convert.ToString(value, CultureInfo.InvariantCulture));
}
Expand Down
2 changes: 2 additions & 0 deletions NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
</ItemGroup>
<ItemGroup>
<Compile Include="LayoutRenderers\AspNetApplicationValueLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetItemValueLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetRequestValueLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetSessionIDLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetSessionValueLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetUserAuthTypeLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\AspNetUserIdentityLayoutRenderer.cs" />
<Compile Include="LayoutRenderers\IISInstanceNameLayoutRenderer.cs" />
<Compile Include="Internal\PropertyReader.cs" />
<Compile Include="NLogHttpModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Targets\AspNetTrace.cs" />
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See [Target documentation at the NLog wiki](https://github.com/NLog/NLog/wiki/Ta
* ${aspnet-application}
* ${aspnet-request}
* ${aspnet-session}
* ${aspnet-item}
* ${aspnet-sessionid}
* ${aspnet-user-authtype}
* ${aspnet-user-identity}
Expand Down

0 comments on commit ce2e477

Please sign in to comment.