-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from NLog/aspnet-item-renderer
Aspnet-item renderer
- Loading branch information
Showing
6 changed files
with
205 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters