From 2cdf4b996cd20cf37508912d5fc5c826aea9cce5 Mon Sep 17 00:00:00 2001 From: Gabriel Sorrel Date: Wed, 6 Feb 2019 18:06:23 -0500 Subject: [PATCH] Changed ExceptionlessAspNetCorePlugin to allow for retrieving HttpContext via IHttpContextAccessor. (#205) Made ExceptionlessAspNetCorePlugin, ExceptionlessDiagnosticListener, and RequestInfoCollector public classes. --- .../ExceptionlessAspNetCorePlugin.cs | 19 +++++++++++++++++-- .../ExceptionlessDiagnosticListener.cs | 2 +- .../ExceptionlessExtensions.cs | 6 +++++- .../RequestInfoCollector.cs | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessAspNetCorePlugin.cs b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessAspNetCorePlugin.cs index 77ad12eb..52a16ed0 100644 --- a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessAspNetCorePlugin.cs +++ b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessAspNetCorePlugin.cs @@ -8,9 +8,23 @@ namespace Exceptionless.AspNetCore { [Priority(90)] - internal class ExceptionlessAspNetCorePlugin : IEventPlugin { + public class ExceptionlessAspNetCorePlugin : IEventPlugin { + IHttpContextAccessor _httpContextAccessor; + + public ExceptionlessAspNetCorePlugin() + : this(null) { + + } + + public ExceptionlessAspNetCorePlugin(IHttpContextAccessor httpContextAccessor) { + _httpContextAccessor = httpContextAccessor; + } + public void Run(EventPluginContext context) { var httpContext = context.ContextData.GetHttpContext(); + if (httpContext == null && _httpContextAccessor != null) + httpContext = _httpContextAccessor.HttpContext; + var serializer = context.Client.Configuration.Resolver.GetJsonSerializer(); if (context.Client.Configuration.IncludeUserName) AddUser(context, httpContext, serializer); @@ -24,7 +38,8 @@ public void Run(EventPluginContext context) { try { ri = httpContext.GetRequestInfo(context.Client.Configuration); - } catch (Exception ex) { + } + catch (Exception ex) { context.Log.Error(typeof(ExceptionlessAspNetCorePlugin), ex, "Error adding request info."); } diff --git a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs index 3c2914e1..5108b513 100644 --- a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs +++ b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DiagnosticAdapter; namespace Exceptionless.AspNetCore { - internal sealed class ExceptionlessDiagnosticListener { + public sealed class ExceptionlessDiagnosticListener { private readonly ExceptionlessClient _client; public ExceptionlessDiagnosticListener(ExceptionlessClient client) { diff --git a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs index f765f770..1839f0df 100644 --- a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs +++ b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs @@ -18,8 +18,12 @@ public static IApplicationBuilder UseExceptionless(this IApplicationBuilder app, if (client == null) client = ExceptionlessClient.Default; + // Can be registered in Startup.ConfigureServices via services.AddHttpContextAccessor(); + // this is necessary to obtain Session and Request information outside of ExceptionlessMiddleware + var contextAccessor = app.ApplicationServices.GetService(); + client.Startup(); - client.Configuration.AddPlugin(); + client.Configuration.AddPlugin(new ExceptionlessAspNetCorePlugin(contextAccessor)); client.Configuration.AddPlugin(); //client.Configuration.Resolver.Register(); diff --git a/src/Platforms/Exceptionless.AspNetCore/RequestInfoCollector.cs b/src/Platforms/Exceptionless.AspNetCore/RequestInfoCollector.cs index 044f6fa8..3bc298b1 100644 --- a/src/Platforms/Exceptionless.AspNetCore/RequestInfoCollector.cs +++ b/src/Platforms/Exceptionless.AspNetCore/RequestInfoCollector.cs @@ -12,7 +12,7 @@ using System.Text; namespace Exceptionless.AspNetCore { - internal static class RequestInfoCollector { + public static class RequestInfoCollector { private const int MAX_BODY_SIZE = 50 * 1024; public static RequestInfo Collect(HttpContext context, ExceptionlessConfiguration config) { if (context == null)