diff --git a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs new file mode 100644 index 00000000..9a591b28 --- /dev/null +++ b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessDiagnosticListener.cs @@ -0,0 +1,51 @@ +using System; +using Exceptionless.Plugins; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DiagnosticAdapter; + +namespace Exceptionless.AspNetCore { + internal sealed class ExceptionlessDiagnosticListener { + private readonly ExceptionlessClient _client; + public ExceptionlessDiagnosticListener(ExceptionlessClient client) { + _client = client; + } + + [DiagnosticName("Microsoft.AspNetCore.Diagnostics.HandledException")] + public void OnDiagnosticHandledException(HttpContext context, Exception ex) { + var contextData = new ContextData(); + contextData.SetSubmissionMethod("Microsoft.AspNetCore.Diagnostics.UnhandledException"); + contextData.Add(nameof(HttpContext), context); + + ex.ToExceptionless(contextData, _client).Submit(); + } + + [DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")] + public void OnDiagnosticUnhandledException(HttpContext context, Exception ex) { + var contextData = new ContextData(); + contextData.MarkAsUnhandledError(); + contextData.SetSubmissionMethod("Microsoft.AspNetCore.Diagnostics.HandledException"); + contextData.Add(nameof(HttpContext), context); + + ex.ToExceptionless(contextData, _client).Submit(); + } + + [DiagnosticName("Microsoft.AspNetCore.Hosting.UnhandledException")] + public void OnHostingUnhandledException(HttpContext context, Exception ex) { + var contextData = new ContextData(); + contextData.MarkAsUnhandledError(); + contextData.SetSubmissionMethod("Microsoft.AspNetCore.Hosting.UnhandledException"); + contextData.Add(nameof(HttpContext), context); + + ex.ToExceptionless(contextData, _client).Submit(); + } + + [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")] + public void OnMiddlewareException(Exception ex, string name) { + var contextData = new ContextData(); + contextData.MarkAsUnhandledError(); + contextData.SetSubmissionMethod(name); + + ex.ToExceptionless(contextData, _client).Submit(); + } + } +} \ No newline at end of file diff --git a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs index 3b8e0d97..f5dccde7 100644 --- a/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs +++ b/src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Exceptionless.AspNetCore; @@ -7,6 +8,7 @@ using Exceptionless.Models.Data; using Exceptionless.Plugins.Default; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; namespace Exceptionless { public static class ExceptionlessExtensions { @@ -18,6 +20,9 @@ public static IApplicationBuilder UseExceptionless(this IApplicationBuilder app, client.Configuration.AddPlugin(); client.Configuration.AddPlugin(); //client.Configuration.Resolver.Register(); + + var diagnosticListener = app.ApplicationServices.GetRequiredService(); + diagnosticListener?.SubscribeWithAdapter(new ExceptionlessDiagnosticListener(client)); return app.UseMiddleware(client); } diff --git a/src/Platforms/Exceptionless.AspNetCore/project.json b/src/Platforms/Exceptionless.AspNetCore/project.json index f4e273ad..f3d1205e 100644 --- a/src/Platforms/Exceptionless.AspNetCore/project.json +++ b/src/Platforms/Exceptionless.AspNetCore/project.json @@ -36,7 +36,9 @@ "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final", "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final", "Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc2-final", - "Microsoft.Net.Http.Headers": "1.0.0-rc2-final" + "Microsoft.Extensions.DiagnosticAdapter": "1.0.0-rc2-final", + "Microsoft.Net.Http.Headers": "1.0.0-rc2-final", + "System.Diagnostics.DiagnosticSource": "4.0.0-rc2-24027" }, "frameworks": { "netstandard1.5": {