This repository was archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Automates MVC and WebApi exception handling #847
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e249d04
Automated MVC WebApi exception handling
f0a1d0a
changelog
3099657
minor fix
619cbbf
review comments, change severity of not-supported-version trace
6d0671a
remove debug.assert change changelog
7178800
up
f241ebe
review
d7e9ae7
Merge branch 'develop' into lmolkova/ExceptionHandling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| namespace Microsoft.ApplicationInsights.Web | ||
| { | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Linq; | ||
| using System.Web.Mvc; | ||
| using Microsoft.ApplicationInsights.Channel; | ||
| using Microsoft.ApplicationInsights.DataContracts; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.ApplicationInsights.Web.Helpers; | ||
| using Microsoft.ApplicationInsights.Web.TestFramework; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| public class MvcExceptionHandlerTests | ||
| { | ||
| private ConcurrentQueue<ExceptionTelemetry> sentTelemetry; | ||
| private TelemetryConfiguration configuration; | ||
|
|
||
| [TestInitialize] | ||
| public void TestInit() | ||
| { | ||
| GlobalFilters.Filters.Clear(); | ||
| this.sentTelemetry = new ConcurrentQueue<ExceptionTelemetry>(); | ||
|
|
||
| var stubTelemetryChannel = new StubTelemetryChannel | ||
| { | ||
| OnSend = t => | ||
| { | ||
| if (t is ExceptionTelemetry telemetry) | ||
| { | ||
| this.sentTelemetry.Enqueue(telemetry); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| this.configuration = new TelemetryConfiguration | ||
| { | ||
| InstrumentationKey = Guid.NewGuid().ToString(), | ||
| TelemetryChannel = stubTelemetryChannel | ||
| }; | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void Cleanup() | ||
| { | ||
| while (this.sentTelemetry.TryDequeue(out var _)) | ||
| { | ||
| } | ||
|
|
||
| GlobalFilters.Filters.Clear(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void MvcExceptionFilterIsInjectedAndTracksException() | ||
| { | ||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var mvcExceptionFilters = GlobalFilters.Filters; | ||
| Assert.AreEqual(1, mvcExceptionFilters.Count); | ||
|
|
||
| var handleExceptionFilter = (HandleErrorAttribute)mvcExceptionFilters.Single().Instance; | ||
| Assert.IsNotNull(handleExceptionFilter); | ||
|
|
||
| var exception = new Exception("test"); | ||
| var controllerCtx = HttpModuleHelper.GetFakeControllerContext(isCustomErrorEnabled: true); | ||
| handleExceptionFilter.OnException(new ExceptionContext(controllerCtx, exception)); | ||
|
|
||
| Assert.AreEqual(1, this.sentTelemetry.Count); | ||
|
|
||
| var trackedException = this.sentTelemetry.Single(); | ||
| Assert.IsNotNull(trackedException); | ||
| Assert.AreEqual(exception, trackedException.Exception); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void MvcExceptionFilterIsNotInjectedIsInjectionIsDisabled() | ||
| { | ||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.EnableMvcAndWebApiExceptionAutoTracking = false; | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| Assert.IsFalse(GlobalFilters.Filters.Any()); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void MvcExceptionLoggerIsNotInjectedIfAnotherInjectionDetected() | ||
| { | ||
| GlobalFilters.Filters.Add(new MvcAutoInjectedFilter()); | ||
| Assert.AreEqual(1, GlobalFilters.Filters.Count); | ||
|
|
||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var filters = GlobalFilters.Filters; | ||
| Assert.AreEqual(1, filters.Count); | ||
| Assert.IsInstanceOfType(filters.Single().Instance, typeof(MvcAutoInjectedFilter)); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void MvcExceptionFilterNoopIfCustomErrorsIsFalse() | ||
| { | ||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var mvcExceptionFilters = GlobalFilters.Filters; | ||
| Assert.AreEqual(1, mvcExceptionFilters.Count); | ||
|
|
||
| var handleExceptionFilter = (HandleErrorAttribute)mvcExceptionFilters.Single().Instance; | ||
| Assert.IsNotNull(handleExceptionFilter); | ||
|
|
||
| var exception = new Exception("test"); | ||
| var controllerCtx = HttpModuleHelper.GetFakeControllerContext(isCustomErrorEnabled: false); | ||
| handleExceptionFilter.OnException(new ExceptionContext(controllerCtx, exception)); | ||
|
|
||
| Assert.IsFalse(this.sentTelemetry.Any()); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void MvcExceptionFilterNoopIfExceptionIsNull() | ||
| { | ||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var mvcExceptionFilters = GlobalFilters.Filters; | ||
| Assert.AreEqual(1, mvcExceptionFilters.Count); | ||
|
|
||
| var handleExceptionFilter = (HandleErrorAttribute)mvcExceptionFilters.Single().Instance; | ||
| Assert.IsNotNull(handleExceptionFilter); | ||
|
|
||
| var controllerCtx = HttpModuleHelper.GetFakeControllerContext(isCustomErrorEnabled: true); | ||
| var exceptionContext = new ExceptionContext(controllerCtx, new Exception()); | ||
| exceptionContext.Exception = null; | ||
| handleExceptionFilter.OnException(exceptionContext); | ||
|
|
||
| Assert.IsFalse(this.sentTelemetry.Any()); | ||
| } | ||
| } | ||
|
|
||
| private class MvcAutoInjectedFilter : HandleErrorAttribute | ||
| { | ||
| public const bool IsAutoInjected = true; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| namespace Microsoft.ApplicationInsights | ||
| { | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Linq; | ||
| using System.Web.Http; | ||
| using System.Web.Http.ExceptionHandling; | ||
| using Microsoft.ApplicationInsights.Channel; | ||
| using Microsoft.ApplicationInsights.DataContracts; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.ApplicationInsights.Web; | ||
| using Microsoft.ApplicationInsights.Web.TestFramework; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| public class WebApiExceptionLoggerTests | ||
| { | ||
| private ConcurrentQueue<ITelemetry> sentTelemetry; | ||
| private TelemetryConfiguration configuration; | ||
|
|
||
| [TestInitialize] | ||
| public void TestInit() | ||
| { | ||
| GlobalConfiguration.Configuration.Services.Clear(typeof(IExceptionLogger)); | ||
| this.sentTelemetry = new ConcurrentQueue<ITelemetry>(); | ||
|
|
||
| var stubTelemetryChannel = new StubTelemetryChannel | ||
| { | ||
| OnSend = t => | ||
| { | ||
| if (t is ExceptionTelemetry telemetry) | ||
| { | ||
| this.sentTelemetry.Enqueue(telemetry); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| this.configuration = new TelemetryConfiguration | ||
| { | ||
| InstrumentationKey = Guid.NewGuid().ToString(), | ||
| TelemetryChannel = stubTelemetryChannel | ||
| }; | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void Cleanup() | ||
| { | ||
| while (this.sentTelemetry.TryDequeue(out var _)) | ||
| { | ||
| } | ||
|
|
||
| GlobalConfiguration.Configuration.Services.Clear(typeof(IExceptionLogger)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WebApiExceptionLoggerIsInjectedAndTracksException() | ||
| { | ||
| Assert.IsFalse(GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).Any()); | ||
|
|
||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var webApiExceptionLoggers = GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).ToList(); | ||
| Assert.AreEqual(1, webApiExceptionLoggers.Count); | ||
|
|
||
| var logger = (ExceptionLogger)webApiExceptionLoggers[0]; | ||
| Assert.IsNotNull(logger); | ||
|
|
||
| var exception = new Exception("test"); | ||
| var exceptionContext = new ExceptionLoggerContext(new ExceptionContext(exception, new ExceptionContextCatchBlock("catch block name", true, false))); | ||
| logger.Log(exceptionContext); | ||
|
|
||
| Assert.AreEqual(1, this.sentTelemetry.Count); | ||
|
|
||
| var trackedException = (ExceptionTelemetry)this.sentTelemetry.Single(); | ||
| Assert.IsNotNull(trackedException); | ||
| Assert.AreEqual(exception, trackedException.Exception); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WebApiExceptionLoggerIsNotInjectedIfAnotherInjectionDetected() | ||
| { | ||
| GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new WebApiAutoInjectedLogger()); | ||
| Assert.AreEqual(1, GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).Count()); | ||
|
|
||
| using (var exceptionModule = new ExceptionTrackingTelemetryModule()) | ||
| { | ||
| exceptionModule.Initialize(this.configuration); | ||
|
|
||
| var loggers = GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).ToList(); | ||
| Assert.AreEqual(1, loggers.Count); | ||
| Assert.IsInstanceOfType(loggers.Single(), typeof(WebApiAutoInjectedLogger)); | ||
| } | ||
| } | ||
|
|
||
| private class WebApiAutoInjectedLogger : ExceptionLogger | ||
| { | ||
| public const bool IsAutoInjected = true; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,26 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <packages> | ||
| <package id="Castle.Core" version="4.2.1" targetFramework="net451" /> | ||
| <package id="Microsoft.ApplicationInsights" version="2.6.0-beta2" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.WebApi" version="5.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.WebApi.Client" version="5.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.WebApi.Core" version="5.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net451" /> | ||
| <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" /> | ||
| <package id="Moq" version="4.8.2" targetFramework="net451" /> | ||
| <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net451" /> | ||
| <package id="OpenCover" version="4.6.519" targetFramework="net451" /> | ||
| <package id="StyleCop.MSBuild" version="5.0.0" targetFramework="net451" developmentDependency="true" /> | ||
| <package id="System.Diagnostics.DiagnosticSource" version="4.4.0" targetFramework="net451" /> | ||
| <package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net451" /> | ||
| <package id="System.ValueTuple" version="4.4.0" targetFramework="net451" /> | ||
| <package id="xunit" version="2.1.0" targetFramework="net451" /> | ||
| <package id="xunit.abstractions" version="2.0.0" targetFramework="net451" /> | ||
| <package id="xunit.assert" version="2.1.0" targetFramework="net451" /> | ||
| <package id="xunit.core" version="2.1.0" targetFramework="net451" /> | ||
| <package id="xunit.extensibility.core" version="2.1.0" targetFramework="net451" /> | ||
| <package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net451" /> | ||
| </packages> | ||
| </packages> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add your comment to "2.6.0-beta3". Beta 2 is being released this week. I can explain offline.