Skip to content

Commit d3b3af0

Browse files
committed
Added custom layout renderer for capturing httpcontext to support async nlog target
1 parent f041241 commit d3b3af0

File tree

6 files changed

+102
-16
lines changed

6 files changed

+102
-16
lines changed

Src/NLog.Targets.Stackify/NLog.Targets.Stackify.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
<RepositoryUrl>https://github.com/stackify/stackify-api-dotnet</RepositoryUrl>
2323
<RepositoryType>git</RepositoryType>
2424
</PropertyGroup>
25-
25+
2626
<ItemGroup>
2727
<ProjectReference Include="..\StackifyLib\StackifyLib.csproj" />
2828
<PackageReference Include="NLog" Version="4.5.0" />
29+
<PackageReference Include="NLog.Config" Version="4.5.0" />
30+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
2931
</ItemGroup>
3032

3133
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
@@ -35,5 +37,17 @@
3537
<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' OR '$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net461' ">
3638
<DefineConstants>NETFULL</DefineConstants>
3739
</PropertyGroup>
40+
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
41+
<Reference Include="System.Web" />
42+
<Reference Include="System.Web.Abstractions" />
43+
<Reference Include="System.Web.Routing" />
44+
<PackageReference Include="NLog.Web" Version="4.5.0" />
45+
</ItemGroup>
46+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
47+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.1" />
48+
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
49+
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.1.1" />
50+
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.0" />
51+
</ItemGroup>
3852

3953
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using NLog.Config;
5+
using NLog.LayoutRenderers;
6+
using NLog.Web.LayoutRenderers;
7+
using StackifyLib.Models;
8+
using Newtonsoft.Json;
9+
10+
namespace NLog.Targets.Stackify
11+
{
12+
[LayoutRenderer("stackify-http")]
13+
public class StackifyHttpLayoutRenderer : AspNetLayoutRendererBase
14+
{
15+
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
16+
{
17+
var httpRequest = HttpContextAccessor.HttpContext;
18+
if (httpRequest != null)
19+
{
20+
#if NETFULL
21+
WebRequestDetail webRequest = new WebRequestDetail();
22+
webRequest.Load(httpRequest);
23+
24+
string result = JsonConvert.SerializeObject(webRequest);
25+
builder.Append(result);
26+
#endif
27+
}
28+
}
29+
}
30+
}

Src/NLog.Targets.Stackify/StackifyTarget.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
268268
stackifyError = StackifyError.New(stringException);
269269
}
270270

271+
if(stackifyError.WebRequestDetail == null && contextProperties.ContainsKey("stackifyhttp"))
272+
{
273+
#if NETFULL
274+
string hctx = contextProperties["stackifyhttp"]?.ToString();
275+
if (!string.IsNullOrEmpty(hctx))
276+
{
277+
var webRequestDetail = Newtonsoft.Json.JsonConvert.DeserializeObject<WebRequestDetail>(hctx);
278+
stackifyError.WebRequestDetail = webRequestDetail;
279+
}
280+
#endif
281+
}
282+
271283
if (stackifyError != null && !StackifyError.IgnoreError(stackifyError) && _logClient.ErrorShouldBeSent(stackifyError))
272284
{
273285
stackifyError.SetAdditionalMessage(loggingEvent.FormattedMessage);

Src/StackifyLib/Internal/Logs/LogQueue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#if NETFULL
1010
using System.Runtime.Remoting.Messaging;
1111
using StackifyLib.Web;
12+
using System.Web;
1213
#endif
1314

1415
namespace StackifyLib.Internal.Logs
@@ -158,7 +159,7 @@ public void QueueLogMessage(Models.LogMsg msg)
158159
}
159160
else
160161
{
161-
var resolver = new RouteResolver(context);
162+
var resolver = new RouteResolver(new HttpContextWrapper(context));
162163

163164
var route = resolver.GetRoute();
164165

Src/StackifyLib/Models/WebRequestDetail.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#if NETFULL
1111
using System.Web;
12-
using System.Web.Routing;
1312
using StackifyLib.Web;
1413

1514
#endif
@@ -25,14 +24,20 @@ public class WebRequestDetail
2524
public static event SetWebRequestDetailEventHandler SetWebRequestDetail;
2625

2726
private StackifyError _Error;
27+
28+
public WebRequestDetail()
29+
{
30+
31+
}
32+
2833
public WebRequestDetail(StackifyError error)
2934
{
3035
_Error = error;
3136

3237
#if NETFULL
3338
if (System.Web.HttpContext.Current != null)
3439
{
35-
Load(System.Web.HttpContext.Current);
40+
Load(new HttpContextWrapper(System.Web.HttpContext.Current));
3641
}
3742
#endif
3843

@@ -93,12 +98,12 @@ public WebRequestDetail(StackifyError error)
9398

9499

95100
#if NETFULL
96-
private void Load(HttpContext context)
101+
public void Load(HttpContextBase context)
97102
{
98103
if (context == null || context.Request == null)
99104
return;
100105

101-
HttpRequest request = context.Request;
106+
HttpRequestBase request = context.Request;
102107

103108
try
104109
{
@@ -248,16 +253,16 @@ internal static void AddKey(string key, string value, Dictionary<string, string>
248253
}
249254

250255
#if NETFULL
251-
internal static Dictionary<string, string> ToKeyValues(HttpCookieCollection collection, List<string> goodKeys, List<string> badKeys)
256+
internal static Dictionary<string, string> ToKeyValues(HttpSessionStateBase collection, List<string> goodKeys, List<string> badKeys)
252257
{
253-
var keys = collection.AllKeys;
258+
var keys = collection.Keys;
254259
var items = new Dictionary<string, string>();
255260

256261
foreach (string key in keys)
257262
{
258263
try
259264
{
260-
HttpCookie cookie = collection[key];
265+
HttpCookie cookie = collection[key] as HttpCookie;
261266

262267
if (cookie != null && !string.IsNullOrWhiteSpace(cookie.Value) && !items.ContainsKey(key))
263268
{
@@ -273,6 +278,32 @@ internal static Dictionary<string, string> ToKeyValues(HttpCookieCollection coll
273278
return items;
274279
}
275280

281+
internal static Dictionary<string, string> ToKeyValues(HttpCookieCollection collection, List<string> goodKeys, List<string> badKeys)
282+
{
283+
var keys = collection.Keys;
284+
var items = new Dictionary<string, string>();
285+
286+
foreach (string key in keys)
287+
{
288+
try
289+
{
290+
HttpCookie cookie = collection[key] as HttpCookie;
291+
292+
if (cookie != null && !string.IsNullOrWhiteSpace(cookie.Value) && !items.ContainsKey(key))
293+
{
294+
AddKey(key, cookie.Value, items, goodKeys, badKeys);
295+
}
296+
}
297+
catch
298+
{
299+
// ignored
300+
}
301+
}
302+
303+
return items;
304+
}
305+
306+
276307
internal static Dictionary<string, string> ToKeyValues(NameValueCollection collection, List<string> goodKeys, List<string> badKeys)
277308
{
278309
var keys = collection.AllKeys;

Src/StackifyLib/Web/RouteResolver.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ public override string ToString()
4242
}
4343
}
4444

45-
private HttpContext _Context = null;
45+
private HttpContextBase _Context = null;
4646

4747
Route _Route = new Route();
4848

4949
public RouteResolver()
50-
: this(System.Web.HttpContext.Current)
50+
: this(new HttpContextWrapper(System.Web.HttpContext.Current))
5151
{
5252
}
5353

54-
public RouteResolver(HttpContext context)
54+
public RouteResolver(HttpContextBase context)
5555
{
5656
_Context = context;
5757

@@ -139,11 +139,9 @@ private Route EvalRoute()
139139
{
140140
if (_Context == null) return route;
141141

142-
var wrapper = new HttpContextWrapper(_Context);
142+
if (_Context == null || RouteTable.Routes == null) return route;
143143

144-
if (wrapper == null || RouteTable.Routes == null) return route;
145-
146-
var routeData = RouteTable.Routes.GetRouteData(wrapper);
144+
var routeData = RouteTable.Routes.GetRouteData(_Context);
147145

148146
if (routeData != null && routeData.Values != null && routeData.Values.Any())
149147
{

0 commit comments

Comments
 (0)