7

社區使用Application API與ASP API核心

我遇到麻煩連接應用程序見解到我的ASP WEB API核心。 遵循標準手冊我仍然無法在我的AppInsights帳戶中找到任何記錄。 我使用了很多手冊,但它們非常相似,並描述瞭如何爲ASP Core(而不是API Core)配置App Insights。 所以我想知道是否需要一些特殊的配置(或nuget包或其他)來使AppInsights跟蹤我的API服務請求?

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

重要提示:我使用VS 2017年RC,網絡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(); 
     } 
+0

只有FYI:沒有ASP.NET Web API Core這樣的東西。 MVC和Web API已合併到ASP.NET Core MVC中。你能告訴我們一些代碼與日誌和你的csproj文件嗎? –

回答

12

,那麼舊的指令是錯誤的,因爲他們爲上基於xproj的asp.net核心實現。

如果您創建一個新 asp.net核心網在VS2017項目,ApplicationInsights將已經從一開始就安裝了,應該是版本:

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

(或更新版本,如果ASP。網核心團隊已經在所有這些更新)

這些項目將已經應用洞察有線起來爲好,不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遷移。網絡核心和應用程序的見解,你還必須刪除的東西,如:

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

從_Layout.cshtml和上面的線替換它們,你可以刪除所有線都喜歡:

應用.UseApplicationInsightsExceptionTelemetry();

在Startup.cs

(如果你使用的軟件包x版本,我相信這些項目將全部顯示廢棄警告爲好,因爲他們不再需要)

VS2017的官方發行說明將此信息包含在"known issues" for application insights

+0

對於MVC web api(VS2017 Web Api項目/ MVC核心1.1),是否只需要在appsettings.json中添加InstrumentationKey? – Dhanuka777

+2

如果你在VS2017中創建了它,那麼是的,AI應該已經包含在項目中,你只需要設置ikey,或者通過在appsettings.json中手動設置,或者右鍵點擊項目,選擇'configure應用程序的見解',並有UI來設置。 –

+0

經過幾個小時的搜索,最終讓我得到了App Insights在Azure的Asp.Net Core Web API應用程序中的工作 - 非常感謝。 – TonE

0

由於此問題已解決問題article

有2點我在配置錯過:
1.在的csproj應參考包「Microsoft.ApplicationInsights.AspNetCore」版本=「2.0.0-β1」
2.在類啓動,方法配置我錯過了添加app.UseApplicationInsightsRequestTelemetry();

當我改變上述AppInsights描述了已經開始。如果你正在使用VS2017的「新」 asp.net核心追蹤所有的請求

+0

我在某處讀到我們應該使用'Program.cs'方法,而不是'Configure'方法來添加它。我有他們兩個,並遙遙看到兩倍。 – nhwilly