Skip to content

May 21, 2015

1

Add Application Insights Exception Handling to MVC 6

by adamstephensen

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
Read more from  Enterprise MVC
1 Comment Post a comment
  1. Tom Rolseth
    Mar 10 2016

    Hey Adam, I created a telemetry class as described above and added an exception in main api controller to see if that would get recorded in App Insights. Unfortunately it did not. I have the instrumentation key in place and the nuget package. I’ve also tried using the following in startup.cs but luck with that either. // Add Application Insights monitoring to the request pipeline as a very first middleware.
    app.UseApplicationInsightsRequestTelemetry();
    // Add Application Insights exceptions handling to the request pipeline.
    app.UseApplicationInsightsExceptionTelemetry();

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments