Skip to content

Commit f1a1428

Browse files
committed
Added the ability to easily set the httpcontext from asp.net core
1 parent 98953f6 commit f1a1428

File tree

9 files changed

+52
-57
lines changed

9 files changed

+52
-57
lines changed

samples/Exceptionless.SampleAspNetCore/Controllers/ValuesController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Exceptionless.Logging;
34
using Microsoft.AspNetCore.Mvc;
45

56
namespace Exceptionless.SampleAspNetCore.Controllers {
@@ -8,6 +9,7 @@ public class ValuesController : Controller {
89
// GET api/values
910
[HttpGet]
1011
public Dictionary<string, string> Get() {
12+
ExceptionlessClient.Default.CreateLog("ValuesController", "Getting results", LogLevel.Info).SetHttpContext(HttpContext).Submit();
1113
throw new Exception($"Random AspNetCore Exception: {Guid.NewGuid()}");
1214
}
1315
}

src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,35 @@ public ExceptionlessDiagnosticListener(ExceptionlessClient client) {
1515
public void OnDiagnosticHandledException(HttpContext httpContext, Exception exception) {
1616
var contextData = new ContextData();
1717
contextData.SetSubmissionMethod("Microsoft.AspNetCore.Diagnostics.UnhandledException");
18-
contextData.Add(nameof(HttpContext), httpContext);
1918

20-
exception.ToExceptionless(contextData, _client).Submit();
19+
exception.ToExceptionless(contextData, _client).SetHttpContext(httpContext).Submit();
2120
}
2221

2322
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")]
2423
public void OnDiagnosticUnhandledException(HttpContext httpContext, Exception exception) {
2524
var contextData = new ContextData();
2625
contextData.MarkAsUnhandledError();
2726
contextData.SetSubmissionMethod("Microsoft.AspNetCore.Diagnostics.HandledException");
28-
contextData.Add(nameof(HttpContext), httpContext);
2927

30-
exception.ToExceptionless(contextData, _client).Submit();
28+
exception.ToExceptionless(contextData, _client).SetHttpContext(httpContext).Submit();
3129
}
3230

3331
[DiagnosticName("Microsoft.AspNetCore.Hosting.UnhandledException")]
3432
public void OnHostingUnhandledException(HttpContext httpContext, Exception exception) {
3533
var contextData = new ContextData();
3634
contextData.MarkAsUnhandledError();
3735
contextData.SetSubmissionMethod("Microsoft.AspNetCore.Hosting.UnhandledException");
38-
contextData.Add(nameof(HttpContext), httpContext);
3936

40-
exception.ToExceptionless(contextData, _client).Submit();
37+
exception.ToExceptionless(contextData, _client).SetHttpContext(httpContext).Submit();
4138
}
4239

4340
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")]
4441
public void OnMiddlewareException(HttpContext httpContext, Exception exception, string name) {
4542
var contextData = new ContextData();
4643
contextData.MarkAsUnhandledError();
4744
contextData.SetSubmissionMethod(name ?? "Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException");
48-
contextData.Add(nameof(HttpContext), httpContext);
4945

50-
exception.ToExceptionless(contextData, _client).Submit();
46+
exception.ToExceptionless(contextData, _client).SetHttpContext(httpContext).Submit();
5147
}
5248
}
5349
}

src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,17 @@ public static Event AddRequestInfo(this Event ev, HttpContext context, Exception
110110
return ev;
111111
}
112112

113-
/// <summary>
114-
/// Adds the current request info as extended data to the event.
115-
/// </summary>
116-
/// <param name="builder">The event builder.</param>
117-
/// <param name="context">The http context to gather information from.</param>
118-
public static EventBuilder AddRequestInfo(this EventBuilder builder, HttpContext context) {
119-
builder.Target.AddRequestInfo(context, builder.Client.Configuration);
120-
return builder;
121-
}
122-
123113
internal static HttpContext GetHttpContext(this IDictionary<string, object> data) {
124-
if (!data.ContainsKey(nameof(HttpContext)))
125-
return null;
114+
object context;
115+
if (data.TryGetValue("HttpContext", out context))
116+
return context as HttpContext;
126117

127-
return data[nameof(HttpContext)] as HttpContext;
118+
return null;
119+
}
120+
121+
public static EventBuilder SetHttpContext(this EventBuilder builder, HttpContext context) {
122+
builder.PluginContextData["HttpContext"] = context;
123+
return builder;
128124
}
129125
}
130126
}

src/Platforms/Exceptionless.AspNetCore/ExceptionlessMiddleware.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ public async Task Invoke(HttpContext context) {
2020
var contextData = new ContextData();
2121
contextData.MarkAsUnhandledError();
2222
contextData.SetSubmissionMethod(nameof(ExceptionlessMiddleware));
23-
contextData.Add(nameof(HttpContext), context);
2423

25-
ex.ToExceptionless(contextData, _client).Submit();
24+
ex.ToExceptionless(contextData, _client).SetHttpContext(context).Submit();
2625
throw;
2726
}
2827

2928
if (context.Response?.StatusCode == 404) {
3029
string path = context.Request.Path.HasValue ? context.Request.Path.Value : "/";
31-
_client.CreateNotFound(path).AddRequestInfo(context).Submit();
30+
_client.CreateNotFound(path).SetHttpContext(context).Submit();
3231
}
3332
}
3433
}
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
-------------------------------------
2-
Exceptionless Readme
2+
Exceptionless Readme
33
-------------------------------------
4-
Exceptionless provides real-time error reporting for your apps. It organizes the
5-
gathered information into simple actionable data that will help your app become
4+
Exceptionless provides real-time error reporting for your apps. It organizes the
5+
gathered information into simple actionable data that will help your app become
66
exceptionless!
77

88
Learn more at http://exceptionless.io.
99

1010
-------------------------------------
11-
How to get an api key
11+
How to get an api key
1212
-------------------------------------
13-
The Exceptionless client requires an api key to use the Exceptionless service.
14-
You can get your Exceptionless api key by logging into http://exceptionless.io
13+
The Exceptionless client requires an api key to use the Exceptionless service.
14+
You can get your Exceptionless api key by logging into http://exceptionless.io
1515
and viewing your project configuration page.
1616

1717
-------------------------------------
18-
ASP.NET Core Integration
18+
ASP.NET Core Integration
1919
-------------------------------------
2020
You must import the "Exceptionless" namespace and call the following line
2121
of code to start reporting unhandled exceptions. The best place to call this
@@ -32,13 +32,20 @@ for examples on sending events to Exceptionless.
3232
-------------------------------------
3333
Manually reporting an exception
3434
-------------------------------------
35-
By default the Exceptionless Client will report all unhandled exceptions. You can
36-
also manually send an exception by importing the Exceptionless namespace and calling
35+
By default the Exceptionless Client will report all unhandled exceptions. You can
36+
also manually send an exception by importing the Exceptionless namespace and calling
3737
the following method.
3838

3939
exception.ToExceptionless().Submit()
4040

41+
Please note that ASP.NET Core doesn't have a static http context. If possible, it is recommended
42+
that you set the HttpContext when submitting events. Doing so will allow the request and
43+
user information to be populated. You can do this by calling the SetHttpContext EventBuilder
44+
extension method.
45+
46+
exception.ToExceptionless().SetHttpContext(ActionContext).Submit()
47+
4148
-------------------------------------
42-
Documentation and Support
49+
Documentation and Support
4350
-------------------------------------
44-
Please visit http://exceptionless.io for documentation and support.
51+
Please visit http://exceptionless.io for documentation and support.

src/Platforms/Exceptionless.WebApi/ExceptionlessExceptionLogger.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ public override void Log(ExceptionLoggerContext context) {
88
var contextData = new ContextData();
99
contextData.MarkAsUnhandledError();
1010
contextData.SetSubmissionMethod("ExceptionLogger");
11-
contextData.Add("HttpActionContext", context.ExceptionContext.ActionContext);
1211

13-
context.Exception.ToExceptionless(contextData).Submit();
12+
context.Exception
13+
.ToExceptionless(contextData)
14+
.SetHttpActionContext(context.ExceptionContext.ActionContext)
15+
.Submit();
1416
}
1517
}
1618
}

src/Platforms/Exceptionless.WebApi/ExceptionlessHandleErrorAttribute.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
namespace Exceptionless.WebApi {
88
public class ExceptionlessHandleErrorAttribute : IExceptionFilter {
99
private readonly ExceptionlessClient _client;
10-
10+
1111
public bool HasWrappedFilter { get { return WrappedFilter != null; } }
1212

1313
public IExceptionFilter WrappedFilter { get; set; }
14-
14+
1515
public bool AllowMultiple { get { return HasWrappedFilter && WrappedFilter.AllowMultiple; } }
1616

1717
public ExceptionlessHandleErrorAttribute(ExceptionlessClient client = null) {
@@ -25,9 +25,11 @@ public virtual void OnHttpException(HttpActionExecutedContext actionExecutedCont
2525
var contextData = new ContextData();
2626
contextData.MarkAsUnhandledError();
2727
contextData.SetSubmissionMethod("ExceptionHttpFilter");
28-
contextData.Add("HttpActionContext", actionExecutedContext.ActionContext);
2928

30-
actionExecutedContext.Exception.ToExceptionless(contextData, _client).Submit();
29+
actionExecutedContext.Exception
30+
.ToExceptionless(contextData, _client)
31+
.SetHttpActionContext(actionExecutedContext.ActionContext)
32+
.Submit();
3133
}
3234

3335
public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) {

src/Platforms/Exceptionless.WebApi/ExceptionlessWebApiExtensions.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,17 @@ public static Event AddHttpRequestInfo(this Event ev, HttpActionContext context,
8686
return ev;
8787
}
8888

89-
/// <summary>
90-
/// Adds the current request info as extended data to the event.
91-
/// </summary>
92-
/// <param name="builder">The event builder.</param>
93-
/// <param name="context">The http action context to gather information from.</param>
94-
[Obsolete("Please call SetHttpActionContext instead.", true)]
95-
public static EventBuilder AddHttpRequestInfo(this EventBuilder builder, HttpActionContext context) {
96-
return builder.SetHttpActionContext(context);
89+
internal static HttpActionContext GetHttpActionContext(this IDictionary<string, object> data) {
90+
object context;
91+
if (data.TryGetValue("HttpActionContext", out context))
92+
return context as HttpActionContext;
93+
94+
return null;
9795
}
9896

9997
public static EventBuilder SetHttpActionContext(this EventBuilder builder, HttpActionContext context) {
10098
builder.PluginContextData["HttpActionContext"] = context;
10199
return builder;
102100
}
103-
104-
internal static HttpActionContext GetHttpActionContext(this IDictionary<string, object> data) {
105-
if (!data.ContainsKey("HttpActionContext"))
106-
return null;
107-
108-
return data["HttpActionContext"] as HttpActionContext;
109-
}
110101
}
111102
}

src/Platforms/Exceptionless.WebApi/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ that you set the HttpActionContext when submitting events. Doing so will allow t
5151
user information to be populated. You can do this by calling the SetHttpActionContext EventBuilder
5252
extension method.
5353

54-
exception.ToExceptionless().SetHttpActionContext(ActionContext).Submit()
54+
exception.ToExceptionless().SetHttpActionContext(ActionContext).Submit()
5555

5656
-------------------------------------
5757
Documentation and Support

0 commit comments

Comments
 (0)