Skip to content

Commit c03720e

Browse files
authored
Merge pull request #423 from WideSpectrumComputing/master
feat: resolve #416
2 parents 1968d98 + 5437ef3 commit c03720e

File tree

5 files changed

+153
-8
lines changed

5 files changed

+153
-8
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
namespace Rollbar.Net.AspNet.WebApi
2+
{
3+
using System.Net.Http;
4+
using System.Net.Http.Headers;
5+
using System.Web.Http.Controllers;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Rollbar.DTOs;
9+
10+
/// <summary>
11+
/// Class HttpActionContextPackageDecorator.
12+
/// Implements the <see cref="Rollbar.RollbarPackageDecoratorBase" />
13+
/// </summary>
14+
/// <seealso cref="Rollbar.RollbarPackageDecoratorBase" />
15+
public class HttpActionContextPackageDecorator
16+
: RollbarPackageDecoratorBase
17+
{
18+
/// <summary>
19+
/// The HTTP action context
20+
/// </summary>
21+
private readonly HttpActionContext _httpActionContext;
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="HttpActionContextPackageDecorator"/> class.
25+
/// </summary>
26+
/// <param name="packageToDecorate">The package to decorate.</param>
27+
/// <param name="httpActionContext">The HTTP action context.</param>
28+
public HttpActionContextPackageDecorator(IRollbarPackage packageToDecorate, HttpActionContext httpActionContext)
29+
: this(packageToDecorate, httpActionContext, false)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="HttpActionContextPackageDecorator"/> class.
35+
/// </summary>
36+
/// <param name="packageToDecorate">The package to decorate.</param>
37+
/// <param name="httpActionContext">The HTTP action context.</param>
38+
/// <param name="mustApplySynchronously">if set to <c>true</c> [must apply synchronously].</param>
39+
public HttpActionContextPackageDecorator(IRollbarPackage packageToDecorate, HttpActionContext httpActionContext, bool mustApplySynchronously)
40+
: base(packageToDecorate, mustApplySynchronously)
41+
{
42+
this._httpActionContext = httpActionContext;
43+
}
44+
45+
/// <summary>
46+
/// Decorates the specified rollbar data.
47+
/// </summary>
48+
/// <param name="rollbarData">The rollbar data.</param>
49+
protected override void Decorate(Data rollbarData)
50+
{
51+
if (this._httpActionContext == null)
52+
{
53+
return; //nothing to do...
54+
}
55+
56+
if (rollbarData.Request == null)
57+
{
58+
rollbarData.Request = new Request();
59+
}
60+
61+
if (rollbarData.Request.Params == null)
62+
{
63+
rollbarData.Request.Params = new Dictionary<string, object>();
64+
}
65+
rollbarData.Request.Params["controller"] =
66+
this._httpActionContext.ControllerContext?.ControllerDescriptor?.ControllerName;
67+
//rollbarData.Request.Params["controller_properties"] =
68+
// this._httpActionContext.ControllerContext?.ControllerDescriptor?.Properties;
69+
rollbarData.Request.Params["action"] =
70+
this._httpActionContext.ActionDescriptor?.ActionName;
71+
//rollbarData.Request["action_properties"] =
72+
// this._httpActionContext.ActionDescriptor?.Properties;
73+
74+
if (this._httpActionContext.Request != null)
75+
{
76+
rollbarData.Request.Url = this._httpActionContext.Request.RequestUri?.PathAndQuery;
77+
rollbarData.Request.Method = this._httpActionContext.Request.Method?.ToString().ToUpper();
78+
rollbarData.Response.Headers = Convert(this._httpActionContext.Request.Headers);
79+
rollbarData.Request.PostBody = ReadInHttpMessageBody(this._httpActionContext.Request.Content);
80+
rollbarData.Request["request_properties"] = this._httpActionContext.Request.Properties;
81+
}
82+
83+
if (this._httpActionContext.Response != null)
84+
{
85+
if (rollbarData.Response == null)
86+
{
87+
rollbarData.Response = new Response();
88+
}
89+
90+
var responseStatusCode = this._httpActionContext.Response.StatusCode;
91+
if (responseStatusCode != null)
92+
{
93+
rollbarData.Response.StatusCode = (int)responseStatusCode;
94+
}
95+
96+
if (this._httpActionContext.Response.Headers != null)
97+
{
98+
rollbarData.Response.Headers = Convert(this._httpActionContext.Response.Headers);
99+
}
100+
101+
rollbarData.Response.Body = ReadInHttpMessageBody(this._httpActionContext.Response.Content);
102+
rollbarData.Response["reason_phrase"] = this._httpActionContext.Response.ReasonPhrase;
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Reads the in HTTP message body.
108+
/// </summary>
109+
/// <param name="httpContent">Content of the HTTP.</param>
110+
/// <returns>System.String.</returns>
111+
private string ReadInHttpMessageBody(HttpContent httpContent)
112+
{
113+
if (httpContent == null)
114+
{
115+
return null;
116+
}
117+
var resultTask = this._httpActionContext.Response?.Content?.ReadAsStringAsync();
118+
if (resultTask == null)
119+
{
120+
return null;
121+
}
122+
resultTask.Wait();
123+
return resultTask.Result;
124+
}
125+
126+
/// <summary>
127+
/// Converts the specified HTTP headers.
128+
/// </summary>
129+
/// <param name="httpHeaders">The HTTP headers.</param>
130+
/// <returns>IDictionary&lt;System.String, System.String&gt;.</returns>
131+
private IDictionary<string, string> Convert(HttpHeaders httpHeaders)
132+
{
133+
if (httpHeaders == null)
134+
{
135+
return null;
136+
}
137+
138+
Dictionary<string, string> headers = new Dictionary<string, string>(httpHeaders.Count());
139+
foreach (var httpHeader in httpHeaders)
140+
{
141+
headers[httpHeader.Key] = httpHeader.Value.Aggregate(string.Empty, (combined, next) => combined += (next + ", "));
142+
}
143+
144+
return headers;
145+
}
146+
}
147+
}

Rollbar.Net.AspNet.WebApi/Rollbar.Net.AspNet.WebApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<Import Project="..\SdkCommon.csproj" />
1010

11-
<PropertyGroup Label="Appned or Override SDK Common NuGet Packaging Info">
11+
<PropertyGroup Label="Append or Override SDK Common NuGet Packaging Info">
1212
<Title>$(Title) for a .NET Framework ASP.NET WebAPI based application.</Title>
1313
<Description>Implements Rollbar Notifier integration with .NET Framework ASP.NET WebAPI based application. $(Description)</Description>
1414
<PackageTags>$(PackageTags);.net-framework;asp.net;webapi</PackageTags>

Rollbar.Net.AspNet.WebApi/RollbarExceptionFilterAttribute.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
namespace Rollbar.Net.AspNet.WebApi
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
6-
using System.Threading;
7-
using System.Threading.Tasks;
83
using System.Web.Http.Filters;
94

105
/// <summary>
@@ -25,6 +20,8 @@ public override void OnException(HttpActionExecutedContext actionExecutedContext
2520
{
2621
IRollbarPackage rollbarPackage =
2722
new ExceptionPackage(actionExecutedContext.Exception, nameof(this.OnException));
23+
rollbarPackage =
24+
new HttpActionContextPackageDecorator(rollbarPackage, actionExecutedContext.ActionContext);
2825

2926
RollbarLocator.RollbarInstance.Critical(rollbarPackage);
3027

SdkCommon.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
feat: resolve #405: Upgrade bench-marking and unit-testing dependencies.
1414
feat: resolve #407: Refactor ScrubFields defaults implementation.
1515
feat: resolve #415: Rollbar.NetCore.AspNet: Add auto-capture of the current HTTP response by the RollbarMiddleware.
16+
feat: resolve #416: Rollbar.Net.AspNet.WebApi: add auto-capture of the current HTTP request/response to RollbarExceptionFilterAttribute.OnException(...) handler.
1617
feat: resolve #417: Rollbar.Net.AspNet.Mvc: add auto-capture of the current HTTP response to RollbarExceptionFilter.
1718
feat: resolve #418: Rollbar.Net.AspNet: add auto-capture of the current HTTP request/response to RollbarHttpModule.
1819
chore: ref #411: Fix CI build .net core 3.0 dependency issue.
19-
chore: ref #412: Fix CI build .net 4.8 target build issue.
20+
chore: resolve #412: Fix CI build .net 4.8 target build issue.
2021
</PackageReleaseNotes>
2122

2223
<!--

UnitTest.Rollbar/TestData/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"TelemetryQueueDepth": 100,
3838
"TelemetryAutoCollectionTypes": "Network, Log, Error",
3939
"TelemetryAutoCollectionInterval": "00:00:00.3000000"
40-
},
40+
}
4141

4242
}

0 commit comments

Comments
 (0)