Skip to content

Commit ad72853

Browse files
committed
added test
1 parent d18ff66 commit ad72853

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Http;
67
using Exceptionless.AspNetCore;
@@ -19,7 +20,10 @@ public static class ExceptionlessExtensions {
1920
/// </summary>
2021
public static IServiceCollection AddExceptionless(this IServiceCollection services) {
2122
services.AddHttpContextAccessor();
22-
services.AddExceptionHandler<ExceptionlessExceptionHandler>();
23+
if (!services.Any(descriptor =>
24+
descriptor.ServiceType == typeof(IExceptionHandler) &&
25+
descriptor.ImplementationType == typeof(ExceptionlessExceptionHandler)))
26+
services.AddExceptionHandler<ExceptionlessExceptionHandler>();
2327
return services;
2428
}
2529

test/Exceptionless.Tests/Platforms/AspNetCoreExceptionCaptureTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@ public void OnNext_WhenUnhandledExceptionEventIsPublished_CapturesUnhandledExcep
6969
Assert.True(submission.IsUnhandledError);
7070
}
7171

72+
[Fact]
73+
public void OnNext_WhenHostingDiagnosticsUnhandledExceptionEventIsPublished_CapturesUnhandledException() {
74+
// Arrange
75+
var submittingEvents = new List<EventSubmittingEventArgs>();
76+
var client = CreateClient(submittingEvents);
77+
var context = CreateHttpContext();
78+
var exception = new InvalidOperationException("unhandled");
79+
var listener = new ExceptionlessDiagnosticListener(client);
80+
81+
// Act
82+
listener.OnNext(new KeyValuePair<string, object>("Microsoft.AspNetCore.Hosting.Diagnostics.UnhandledException", new {
83+
httpContext = context,
84+
exception
85+
}));
86+
87+
// Assert
88+
var submission = Assert.Single(submittingEvents);
89+
Assert.True(submission.IsUnhandledError);
90+
}
91+
92+
[Fact]
93+
public void OnNext_WhenMiddlewareExceptionPayloadIsNull_DoesNotThrowOrCaptureException() {
94+
// Arrange
95+
var submittingEvents = new List<EventSubmittingEventArgs>();
96+
var client = CreateClient(submittingEvents);
97+
var listener = new ExceptionlessDiagnosticListener(client);
98+
99+
// Act
100+
listener.OnNext(new KeyValuePair<string, object>("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException", null));
101+
102+
// Assert
103+
Assert.Empty(submittingEvents);
104+
}
105+
72106
[Fact]
73107
public async Task Invoke_WhenResponseStatusIsNotFound_SubmitsNotFoundEvent() {
74108
// Arrange

0 commit comments

Comments
 (0)