无法将 ASP.NET 核心 Web 应用与 Application Insights 连接

Cannot connect ASP.NET Core web app with Application Insights

我有 ASP.NET 核心 MVC 项目(其中一个作为控制台应用程序启动的新奇事物)在 Visual Studio 社区 2015 更新 3 托管在 Azure 应用程序服务上。我正在尝试设置 Application Insights,主要是因为我想使用 ApplicationInsightsTraceListener 捕获诊断跟踪,但其他功能也很有用。我还没有偶然发现正确的配置来让任何东西都显示在 Azure 门户中。

我使用 Visual Studio 扩展为我的项目添加了 Application Insights 支持。然而,这似乎是事情开始出错的地方,它 而不是 创建一个 ApplicationInsights.config。它添加了一些 DLL,并将 InstrumentationKey 放入我的应用程序设置文件中,并且我通过 NuGet 手动添加了更多程序集,包括 Microsoft.ApplicationInsights.WebMicrosoft.ApplicationInsights.TraceListener。此时发布会导致 Azure 门户中未启用任何指标,实时流告诉我 不可用:您的应用程序处于离线状态或使用较旧的 SDK

我已经尝试了 2.1.0 和 2.2.0 beta。我尝试删除所有 Application Insights 内容,重新安装扩展并重新开始。我也尝试跳过使用扩展,手动添加 Microsoft.ApplicationInsights.Web 等,一些 google 结果暗示也应该创建配置文件,但没有运气。我已经从网上下载了其他人的 ApplicationInsights.config 文件,并试图让它们适合我的环境。门户的 Application Insights 部分从来没有任何工作。

除了创建配置文件之外,扩展还应该采取哪些额外步骤?我需要向 Startup.cs 添加代码吗?大多数文档似乎都认为初始设置有效。

您需要修改您的 Startup.cs,考虑到您的 InstrumentationKeyappsettings.json:

public void ConfigureServices(IServiceCollection services)
{
    /*Simple Mvc web app*/
    services.AddMvc();

    services.AddApplicationInsightsTelemetry(Configuration);
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
    /*This should be before UseMvc*/
    app.UseApplicationInsightsRequestTelemetry();
    app.UseApplicationInsightsExceptionTelemetry();
    app.UseMvc();


}

重要的是app.UseApplicationInsightsRequestTelemetry()app.UseMvc()之前

ASP.NET 核心项目有单独的 AI SDK,您可以找到 project on GitHub(ApplicationInsights-aspnetcore) and the Nuget package on Nuget.org (Microsoft.ApplicationInsights.AspNetCore)

要开始收集,应将 AI 中间件连接到项目代码中(一旦添加了适当的 Nuget 包),即 Request 和 Exception 收集中间件。应该在启动时添加一个额外的步骤来构建 AI 配置。

Getting Started on GitHub project should cover this, but I found it empty today, however, Configure 经验提到如何添加 InstrumentationKey 和一些额外的集合,例如对于基于配置的方法来设置 IKey:

如果您使用的是 json 配置提供程序 - 将以下内容添加到 config.json

    "ApplicationInsights": {
    "InstrumentationKey": "11111111-2222-3333-4444-555555555555"
}

如果您正在使用环境变量:

SET ApplicationInsights:InstrumentationKey=11111111-2222-3333-4444-555555555555

注意:不支持由 azure 网站 (APPINSIGHTS_INSTRUMENTATIONKEY) 设置的环境变量。

或您在应用程序中定义的任何其他配置提供程序格式(配置设置名称为 ApplicationInsights:InstrumentationKey),然后调用以在 Startup.cs 中添加见解遥测:

services.AddApplicationInsightsTelemetry(Configuration);

UseApplicationInsightsExceptionTelemetry 应该在 UseMvc 之前。如果我在 UseMvc 之后使用它,它不会保存异常,我只看到请求。当然你还应该添加 nuget 包 "Microsoft.ApplicationInsights.AspNetCore" 并添加到 appsettings.json:

      "ApplicationInsights": {
    "InstrumentationKey": "xxxxxxx-xxxxx-xxxx-xxx-xxxxxxxxxx"
  }

我的 Startup.cs 看起来是这样的:

    public Startup(IHostingEnvironment env)
    {
        ...
        if (env.IsDevelopment())
        {
            builder.AddApplicationInsightsSettings(developerMode: true);
        }
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddMvc();
        ....
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();
    }