Skip to content

Archive for

21
May

Add Application Insights Exception Handling to MVC 6

While everyone is making the move to MVC 6 (and it is pretty awesome), not everything is there yet.

Until it works out of the box, here is an easy way to wire up your MVC 6 project to log all exceptions to Application Insights.

Create the Application Insights resource via the Azure Portal

  1. Sign in to the Azure portal, and create a new Application Insights resource. Choose ASP.NET as the application type.

Click New, Application Insights

A resource in Azure is an instance of a service. This resource is where telemetry from your app will be analyzed and presented to you.

The choice of application type sets the default content of the resource blades and the properties visible in Metrics Explorer.

2. Take a copy of the Instrumentation Key.

The key identifies the resource, and you’ll install it soon in the SDK to direct data to the resource.

Click Properties, select the key, and press ctrl+C

Create the Exception Filter to log all exceptions

2015-05-21_11-32-29

Figure: Add the ApplicationInsights.Web NuGet package

2015-05-21_11-21-27_01

Figure: Create the Telemetry class to instantiate an instance of the TelemetryClient with your InstrumentationKey & the AiHandleErrorAttribute class that will log exceptions to Application Insights.

2015-05-21_11-25-18

Figure: Wire up the ExceptionFilterAttribute in Startup.cs

Advertisements