Asp.net-Web-Api

將 Application Insight 與 ASP API 核心一起使用

  • March 14, 2017

社區

我在將 Application Insights 連接到我的 ASP WEB API 核心時遇到問題。按照標準手冊,我仍然無法在我的 AppInsights 帳戶中找到任何記錄。我使用了很多手冊,但它們幾乎完全相同,並描述瞭如何為 ASP Core(而不是 API Core)配置 App Insights。所以我想知道是否需要一些特殊配置(或 nuget 包或其他)來使 AppInsights 跟踪對我的 API 服務的請求?

一旦我無法使 AppInsights 開箱即用,我仍然可以創建 TelemetryClient 實例並手動發布數據,但這在我的情況下是不可取的。

重要提示:我使用的是 VS 2017 RC,Web APi Core 項目 (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();
       }

如果您在 VS2017 中使用“新”的 asp.net 核心,那麼舊指令是錯誤的,因為它們適用於以前基於 xproj 的 asp.net 核心實現。

如果您在 VS2017 中創建一個的asp.net core 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) 

從 _Layout.cshtml 並將它們替換為上面的行,您可以刪除所有行,例如:

app.UseApplicationInsightsExceptionTelemetry();

在 Startup.cs 中(如果您使用的是 2.x 版本的軟體包,我相信這些項目也會顯示棄用警告,因為它們不再需要)

VS2017 的官方發布說明將此資訊作為“已知問題”的一部分包含在應用程序洞察中

引用自:https://stackoverflow.com/questions/42513401