ASP.NET Core MVC application insights stopped working after upgrade to .NET Core 1.1.1
.net, asp.net-core, asp.net-core-mvc, azure-application-insights, c#
Solution
There are breaking changes from ApplicationInsights 1.x to 2.x which are documented on GitHub release notes.
- This release contains a rewrite of the SDK internals for better .NET Core integration and initialization.
- `UseApplicationInsightsRequestTelemetry` is obsolete, the logic it used to perform is handled automatically now and calls to this method should be deleted from Startup.cs.
- `UseApplicationInsightsExceptionTelemetry` is obsolete, exception telemetry is handled automatically internally now. You should delete calls to this method from Startup.cs otherwise you will get duplicate exception telemetry reported.
- The MVC dependency for the JavaScript snippet has been removed so in order to include the JavaScript snippet now you need to make the following changes:
- In _ViewImports.cshtml replace `@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration` with `@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet`
- In _Layout.cshtml replace `@Html.ApplicationInsightsJavaScript(TelemetryConfiguration)` with `@Html.Raw(JavaScriptSnippet.FullScript)`
Problem
I have one simple ASP.NET Core application I was working on back in December 2016. It worked just fine with application insights and telemetry. Now after 4 months I wanted to pick up this work and started with upgrade from .NET Core 1.1.0 to 1.1.1. In this process package `Microsoft.ApplicationInsights.AspNetCore` got updated from version `1.0.2` to version `2.0.0`. This unfortunately caused my app to stop working, in particular I get this error: ``` An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. /Views/Shared/_Layout.cshtml 'IHtmlHelper<dynamic>' does not contain a definition for 'ApplicationInsightsJavaScript' and no extension method 'ApplicationInsightsJavaScript' accepting a first argument of type 'IHtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?) + @Html.ApplicationInsightsJavaScript(TelemetryConfiguration) Show compilation source #pragma checksum "/Views/Shared/_Layout.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "050a2afdfb4a44e17be72e76627a3d3a0d0b7d5a" namespace AspNetCore { #line 1 "/Views/_ViewImports.cshtml" ``` Screen: Upgrade from project.json to new csproj and using new Visual Studio 2017 doesn't help. It looks like ApplicationInsightsJavaScript was basically removed from API. How do I enable javascript application insights then?