将 Application Insight 与 ASP API 核心一起使用

Use Application Insight with ASP API Core

社区

我在将 Application Insights 连接到我的 ASP WEB API Core 时遇到问题。 按照标准手册,我仍然无法在我的 AppInsights 帐户中找到任何记录。 我使用了很多手册,但它们非常相同,并且描述了如何为 ASP Core(而非 API Core)配置 App Insights。 所以我想知道是否需要一些特殊配置(或 nuget 包或其他)才能使 AppInsights 跟踪对我的 API 服务的请求?

一旦我无法让 AppInsights 开箱即用,我仍然可以创建 TelemetryClient 的实例并手动发布数据,但这在我的情况下是不可取的。

重要说明:我使用的是 VS 2017 RC,Web APi 核心项目 (csproj)

UPD

csproj文件内容:

  <Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
  <ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-msbuild1-update1" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild2-update1" />
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-msbuild1-update1" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild2-update1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.1" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.2.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild1-final" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="1.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0-msbuild2-final" />
    <PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.1" />
    <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
    <PackageReference Include="Swashbuckle.SwaggerGen" Version="6.0.0-beta901" />
    <PackageReference Include="Swashbuckle.SwaggerUi" Version="6.0.0-beta901" />
  </ItemGroup>
</Project>

Startup.cs中的配置:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(true);
            }

            builder.AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddApplicationInsightsTelemetry(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug(LogLevel.Trace);
            loggerFactory.AddConsole(LogLevel.Error);

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseMvc();
        }

这个问题解决了article

我的配置漏掉了两点:
1.在csproj中应该引用package "Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0-beta1"
2. 在 class Startup 中,方法 Configure 我错过了添加 app.UseApplicationInsightsRequestTelemetry();

当我更改上述内容时,AppInsights 已开始跟踪所有请求

如果您在 VS2017 中使用 "new" asp.net 核心,那么旧指令是错误的,因为它们适用于之前基于 xproj 的 asp.net 核心实现。

如果您在 VS2017 中创建 new asp.net 核心 Web 项目,ApplicationInsights 将从一开始就已经安装,版本应该是:

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />

(或更新,如果 asp.net 核心团队已经更新了它们)

这些项目也已经连接了 Application Insights,不是在 Startup.cs 中(这是旧方法),而是在 Program.cs:

new WebHostBuilder()
   ...
   .UseApplicationInsights() // this starts up appinsights in asp.net core now
   ...
   .UseOtherThings();

并且可能在网络模板中,例如:

 @inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

在顶部,

 @Html.Raw(JavaScriptSnippet.FullScript)

<head> 标签的底部。

如果您要从以前版本的 asp.net 核心和应用见解迁移,您还必须删除以下内容:

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

from _Layout.cshtml 并将它们替换为上面的行,您可以删除所有行,例如:

app.UseApplicationInsightsExceptionTelemetry();

在 Startup.cs 中(如果您使用的是 2.x 版本的软件包,我相信这些项目也会显示弃用警告,因为它们不再需要)

VS2017 的官方发行说明将此信息作为 "known issues" for application insights

中的一个部分