ASP.NET 核心 MVC 应用程序见解在升级到 .NET Core 1.1.1 后停止工作

ASP.NET Core MVC application insights stopped working after upgrade to .NET Core 1.1.1

我在 2016 年 12 月开发了一个简单的 ASP.NET 核心应用程序。它在应用程序洞察和遥测方面运行良好。

现在 4 个月后我想接手这项工作并开始从 .NET Core 1.1.0 升级到 1.1.1。在此过程中,包 Microsoft.ApplicationInsights.AspNetCore 从版本 1.0.2 更新到版本 2.0.0

不幸的是,这导致我的应用程序停止工作,特别是我收到此错误:

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"

屏幕:

从 project.json 升级到新的 csproj 并使用新的 Visual Studio 2017 没有帮助。

看起来 ApplicationInsightsJavaScript 基本上已从 API 中删除。那么如何启用 javascript 应用程序洞察力?

从 ApplicationInsights 1.x 到 2.x 有重大变化,记录在 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)