Skip to content

Commit 2cdf4b9

Browse files
moogle001niemyjski
authored andcommitted
Changed ExceptionlessAspNetCorePlugin to allow for retrieving HttpContext via IHttpContextAccessor. (#205)
Made ExceptionlessAspNetCorePlugin, ExceptionlessDiagnosticListener, and RequestInfoCollector public classes.
1 parent c03b5d3 commit 2cdf4b9

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/Platforms/Exceptionless.AspNetCore/ExceptionlessAspNetCorePlugin.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@
88

99
namespace Exceptionless.AspNetCore {
1010
[Priority(90)]
11-
internal class ExceptionlessAspNetCorePlugin : IEventPlugin {
11+
public class ExceptionlessAspNetCorePlugin : IEventPlugin {
12+
IHttpContextAccessor _httpContextAccessor;
13+
14+
public ExceptionlessAspNetCorePlugin()
15+
: this(null) {
16+
17+
}
18+
19+
public ExceptionlessAspNetCorePlugin(IHttpContextAccessor httpContextAccessor) {
20+
_httpContextAccessor = httpContextAccessor;
21+
}
22+
1223
public void Run(EventPluginContext context) {
1324
var httpContext = context.ContextData.GetHttpContext();
25+
if (httpContext == null && _httpContextAccessor != null)
26+
httpContext = _httpContextAccessor.HttpContext;
27+
1428
var serializer = context.Client.Configuration.Resolver.GetJsonSerializer();
1529
if (context.Client.Configuration.IncludeUserName)
1630
AddUser(context, httpContext, serializer);
@@ -24,7 +38,8 @@ public void Run(EventPluginContext context) {
2438

2539
try {
2640
ri = httpContext.GetRequestInfo(context.Client.Configuration);
27-
} catch (Exception ex) {
41+
}
42+
catch (Exception ex) {
2843
context.Log.Error(typeof(ExceptionlessAspNetCorePlugin), ex, "Error adding request info.");
2944
}
3045

src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Extensions.DiagnosticAdapter;
55

66
namespace Exceptionless.AspNetCore {
7-
internal sealed class ExceptionlessDiagnosticListener {
7+
public sealed class ExceptionlessDiagnosticListener {
88
private readonly ExceptionlessClient _client;
99

1010
public ExceptionlessDiagnosticListener(ExceptionlessClient client) {

src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public static IApplicationBuilder UseExceptionless(this IApplicationBuilder app,
1818
if (client == null)
1919
client = ExceptionlessClient.Default;
2020

21+
// Can be registered in Startup.ConfigureServices via services.AddHttpContextAccessor();
22+
// this is necessary to obtain Session and Request information outside of ExceptionlessMiddleware
23+
var contextAccessor = app.ApplicationServices.GetService<IHttpContextAccessor>();
24+
2125
client.Startup();
22-
client.Configuration.AddPlugin<ExceptionlessAspNetCorePlugin>();
26+
client.Configuration.AddPlugin(new ExceptionlessAspNetCorePlugin(contextAccessor));
2327
client.Configuration.AddPlugin<IgnoreUserAgentPlugin>();
2428
//client.Configuration.Resolver.Register<ILastReferenceIdManager, WebLastReferenceIdManager>();
2529

src/Platforms/Exceptionless.AspNetCore/RequestInfoCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using System.Text;
1313

1414
namespace Exceptionless.AspNetCore {
15-
internal static class RequestInfoCollector {
15+
public static class RequestInfoCollector {
1616
private const int MAX_BODY_SIZE = 50 * 1024;
1717
public static RequestInfo Collect(HttpContext context, ExceptionlessConfiguration config) {
1818
if (context == null)

0 commit comments

Comments
 (0)