-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathAspNetRequestValueLayoutRenderer.cs
140 lines (128 loc) · 4.3 KB
/
AspNetRequestValueLayoutRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Text;
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
#endif
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:header=h}
/// ${aspnet-request:serverVariable=v}
/// </code>
/// </example>
[LayoutRenderer("aspnet-request")]
public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <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>
/// Gets or sets the Headers item to be rendered.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public string Header { 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 DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor.HttpContext.Request;
if (httpRequest == null)
{
return;
}
if (this.QueryString != null)
{
#if !DNX
builder.Append(httpRequest.QueryString[this.QueryString]);
#else
builder.Append(httpRequest.Query[this.QueryString]);
#endif
}
else if (this.Form != null)
{
builder.Append(httpRequest.Form[this.Form]);
}
else if (this.Cookie != null)
{
#if !DNX
var cookie = httpRequest.Cookies[this.Cookie];
if (cookie != null)
{
builder.Append(cookie.Value);
}
#else
var cookie = httpRequest.Cookies[this.Cookie];
builder.Append(cookie);
#endif
}
else if (this.ServerVariable != null)
{
#if !DNX
builder.Append(httpRequest.ServerVariables[this.ServerVariable]);
#else
throw new NotSupportedException();
#endif
}
else if (this.Header != null)
{
string header = httpRequest.Headers[this.Header];
if (header != null)
{
builder.Append(header);
}
}
else if (this.Item != null)
{
#if !DNX
builder.Append(httpRequest[this.Item]);
#else
builder.Append(httpRequest.HttpContext.Items[this.Item]);
#endif
}
}
}
}