Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move stuff from extended #2

Merged
9 commits merged into from Jan 31, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion NLog.Web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLog.Web", "NLog.Web/NLog.Web.csproj", "{E30DC886-8431-4CFA-90FA-38D9BE4203A0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLog.Web", "NLog.Web\NLog.Web.csproj", "{E30DC886-8431-4CFA-90FA-38D9BE4203A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
73 changes: 73 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Globalization;
using System.Text;
using System.Web;
using NLog.Config;
using NLog.LayoutRenderers;

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

/// <summary>
/// Renders the specified ASP.NET Application variable 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 (this.Variable == null)
{
return;
}

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

if (context.Application == null)
{
return;
}

builder.Append(Convert.ToString(context.Application[this.Variable], CultureInfo.InvariantCulture));
}
}
}
104 changes: 104 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Text;
using System.Web;
using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET Request variable.
/// </summary>
/// <remarks>
/// Use this layout renderer to insert the value of the specified parameter of the
/// ASP.NET Request object.
/// </remarks>
/// <example>
/// <para>Example usage of ${aspnet-request}:</para>
/// <code lang="NLog Layout Renderer">
/// ${aspnet-request:item=v}
/// ${aspnet-request:querystring=v}
/// ${aspnet-request:form=v}
/// ${aspnet-request:cookie=v}
/// ${aspnet-request:serverVariable=v}
/// </code>
/// </example>
[LayoutRenderer("aspnet-request")]
public class AspNetRequestValueLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Gets or sets the item name. The QueryString, Form, Cookies, or ServerVariables collection variables having the specified name are rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
[DefaultParameter]
public string Item { get; set; }

/// <summary>
/// Gets or sets the QueryString variable to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string QueryString { get; set; }

/// <summary>
/// Gets or sets the form variable to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string Form { get; set; }

/// <summary>
/// Gets or sets the cookie to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string Cookie { get; set; }

/// <summary>
/// Gets or sets the ServerVariables item to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string ServerVariable { get; set; }

/// <summary>
/// Renders the specified ASP.NET Request variable 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)
{
HttpContext context = HttpContext.Current;
if (context == null)
{
return;
}

if (context.Request == null)
{
return;
}

if (this.QueryString != null)
{
builder.Append(context.Request.QueryString[this.QueryString]);
}
else if (this.Form != null)
{
builder.Append(context.Request.Form[this.Form]);
}
else if (this.Cookie != null)
{
HttpCookie cookie = context.Request.Cookies[this.Cookie];

if (cookie != null)
{
builder.Append(cookie.Value);
}
}
else if (this.ServerVariable != null)
{
builder.Append(context.Request.ServerVariables[this.ServerVariable]);
}
else if (this.Item != null)
{
builder.Append(context.Request[this.Item]);
}
}
}
}
34 changes: 34 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text;
using System.Web;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET Session ID.
/// </summary>
[LayoutRenderer("aspnet-sessionid")]
public class AspNetSessionIDLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Renders the ASP.NET Session ID 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)
{
HttpContext context = HttpContext.Current;
if (context == null)
{
return;
}

if (context.Session == null)
{
return;
}

builder.Append(context.Session.SessionID);
}
}
}
72 changes: 72 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Globalization;
using System.Text;
using System.Web;
using NLog.Config;
using NLog.LayoutRenderers;

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

/// <summary>
/// Renders the specified ASP.NET Session 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 (this.Variable == null)
{
return;
}

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

if (context.Session == null)
{
return;
}

builder.Append(Convert.ToString(context.Session[this.Variable], CultureInfo.InvariantCulture));
}
}
}
44 changes: 44 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text;
using System.Web;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET User variable.
/// </summary>
[LayoutRenderer("aspnet-user-authtype")]
public class AspNetUserAuthTypeLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Renders the specified ASP.NET User.Identity.AuthenticationType variable 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)
{
HttpContext context = HttpContext.Current;
if (context == null)
{
return;
}

if (context.User == null)
{
return;
}

if (context.User.Identity == null)
{
return;
}

if (!context.User.Identity.IsAuthenticated)
{
return;
}

builder.Append(context.User.Identity.AuthenticationType);
}
}
}
39 changes: 39 additions & 0 deletions NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text;
using System.Web;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET User variable.
/// </summary>
[LayoutRenderer("aspnet-user-identity")]
public class AspNetUserIdentityLayoutRenderer : LayoutRenderer
{
/// <summary>
/// Renders the specified ASP.NET User.Identity.Name variable 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)
{
var context = HttpContext.Current;
if (context == null)
{
return;
}

if (context.User == null)
{
return;
}

if (context.User.Identity == null)
{
return;
}

builder.Append(context.User.Identity.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Web.Hosting;
using NLog.LayoutRenderers;

namespace NLog.Web
namespace NLog.Web.LayoutRenderers
{
[LayoutRenderer("iis-site-name")]
public class IISInstanceNameLayoutRenderer : LayoutRenderer
Expand Down
Loading