无法使用Application Insights连接ASP.NET Core Web应用程序

时间:2016-09-01 14:20:20

标签: asp.net asp.net-mvc azure asp.net-core azure-application-insights

我在Azure App Services上托管的Visual Studio Community 2015 Update 3中有ASP.NET核心MVC项目(这些新奇的东西之一,作为控制台应用程序启动)。我尝试设置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等人,谷歌的一些结果暗示也应该创建配置文件,但没有运气。我从网上下载了其他人的ApplicationInsights.config文件,并试图让他们对我的环境更敏感。在任何情况下,门户网站的Application Insights部分都没有任何工作。

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

3 个答案:

答案 0 :(得分:3)

您需要修改Startup.cs,考虑到InstrumentationKey上有appsettings.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()之前非常重要。

答案 1 :(得分:2)

ASP.NET Core项目有一个单独的AI SDK,您可以找到project on GitHub(ApplicationInsights-aspnetcore)和Nuget package on Nuget.org(Microsoft.ApplicationInsights.AspNetCore)

要启动集合,应将AI中间件连接到项目代码中(一旦添加了相应的Nuget包),即Request和Exception集合中间件。应该在启动时添加构建AI配置的附加步骤。

GitHub项目中的

Getting Started应该涵盖这一点,但我今天发现它是空的,但是,Configure经验提到了如何添加InstrumentationKey和一些额外的集合,例如用于设置IKey的基于配置的方法:

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

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

如果您使用的是环境变量:

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

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

或者,在调用Startup.cs中添加洞察遥测之前,您在应用程序中定义的任何其他配置提供程序格式(配置设置名称为ApplicationInsights:InstrumentationKey):

services.AddApplicationInsightsTelemetry(Configuration);

答案 2 :(得分:0)

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();
    }