Skip to content

advancedrei/ApplicationInsights.Helpers

Repository files navigation

ApplicationInsights.Helpers

Helpers for MVC 5 and WebAPI 2, to make your AI life easier.

Application Insights from Microsoft is a great little system. The fact that it's free doesn't hurt either. But it takes quite a bit of setup to get going, and when it comes to things like Exception handling, you don't find out that it doesn't cover all exceptions unless you stumble upon a blog post with the changes you need to make to your code.

I wanted to make it easier to use Application Insights, and this does just that. Why Microsoft's didn't do these things themselves is the real question, but they are free to add this code to the SDK (which AFAIK is not open source).

Goals:

  • Add the requisite JavaScript to the MVC _layout.cshtml without having to worry about the actual script.
  • Enable ApplicationInsights to log to different web "apps" based on Environment, without dealing with config transforms.
  • Make it work with Microsoft's recommendation for a single instance of the TelemetryClient.
  • Implement the proper handlers to grab every exception.
  • Make it work with Autofac (and other DI containers).

Installation:

All versions:

  1. In the Application Insights installer in Visual Studio has not done so already, add the following line to your appSettings in web.config, or to your Azure Web App appSettings:
<add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="**InsertYourInstrumentationGuidHere**"/>

MVC: 2) Install the NuGet package: Install-Package ApplicationInsights.Helpers.Mvc5 -pre.

  1. Add the following code to DependencyConfig.cs:
builder.RegisterType<TelemetryClient>().SingleInstance();
builder.Register(c => new InsightsHandleErrorAttribute(c.Resolve<TelemetryClient>())).AsExceptionFilterFor<Controller>().InstancePerRequest();
builder.RegisterFilterProvider();
  1. Add the following code to the first line of either Register() in Startup.cs, or Application_Start() in Global.asax.cs:
TelemetryConfiguration.Active.ContextInitializers.Add(new InsightsTelemetryInitializer());
  1. Add the following to the first line of the tag in Shared/_Layout.cshtml:
@Html.AddApplicationInsightsHeader()

WebApi:

  1. Install the NuGet package: Install-Package ApplicationInsights.Helpers.WebApi2 -pre.

  2. Add the following code to the first line of Register() in WebApiConfig.cs:

TelemetryConfiguration.Active.ContextInitializers.Add(new InsightsTelemetryInitializer());
config.Services.Add(typeof(IExceptionLogger), new InsightsExceptionLogger(new TelemetryClient())); 

NOTE: Not sure how to register the dependency in Autofac and then resolve the instance outside of constructor injection, so if you have a better way to do this and make the TelemetryClient instance available to the whole app, I'm all ears.

That's it! Now you can have slot-specific InstrumentationKeys, all of your exceptions are captured, TelemetryClient instances are used, and life is grand.

Questions?

Tweet me at @robertmclaws.

About

Helpers for MVC 5 and WebAPI 2, to make your AI life easier.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published