7

コミュニティApplication Insight with ASP APIコアを使用する

私のASP WEB APIコアにApplication Insightを接続する際に問題が発生しています。 標準のマニュアルに従って、私はまだAppInsightsアカウントでレコードを見つけることができません。 私は多くのマニュアルを使用しましたが、かなり同じで、ASPコア(APIコアではない)のためのApp Insightの設定方法を説明しています。 AppInsightsのリクエストをAPIサービスに追跡させるためには、特別な設定(または、ナゲットパッケージなど)が必要なのでしょうか?

AppInsightsをすぐに使えないようにしても、TelemetryClientのインスタンスを作成して手動でデータを公開することはできますが、これは私の場合は望ましくありません。

重要な注意:私はVS 2017 RC、Web APIをコアプロジェクト(csproj)

UPD

csprojファイルの内容を使用しています:Startup.csで

<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> 

構成:

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コアなどはありません。 MVCとWeb APIがASP.NET Core MVCに統合されました。ロギングとcsprojファイルでいくつかのコードを表示できますか? –

答えて

12

を追跡するために開始したAppInsights、上記のものを変更した場合、彼らは以前のためのものとして

は、古い命令は、間違っていますxprojベースのasp.netコア実装。

あなたはVS2017で新しい asp.netコアのWebプロジェクトを作成する場合は、ApplicationInsightsはすでに最初からインストールされ、バージョンのようになります。

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

(以降、ASPの場合。

:ネットコアチームは

これらのプロジェクトは、既にアプリケーション洞察力がない(つまり、古い方法です)Startup.csではなく、Program.csの中だけでなく、最大の有線きます)で、すべてのそれらを更新しました3210

new WebHostBuilder() 
    ... 
    .UseApplicationInsights() // this starts up appinsights in asp.net core now 
    ... 
    .UseOtherThings(); 
などのWebテンプレート内

そしておそらく、:上部の

@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet 

、および<head>タグの内底部

@Html.Raw(JavaScriptSnippet.FullScript) 

以前のバージョンのaspから移行する場合は、ネットコアとアプリ洞察は、あなたも同じようなものを削除する必要があります:_Layout.cshtmlから

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

と上記の行に置き換えると、あなたはすべての行を削除することができますように:

アプリを.UseApplicationInsightsExceptionTelemetry(); Startup.cs(あなたがパッケージの2.xのバージョンを使用している場合、私はそれらが不要になっているとして、これらの項目は、すべてのshow非推奨の警告は、同様と考えている)

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

検索の時間が経ってから、私はAzureのAsp.Net Core Web APIアプリケーションのためにApp Insightを働かせました。 – TonE

0

この問題のおかげで、article

私は私の構成で逃した2ポイントがあります。csprojで
1.参照すべきパッケージ「Microsoft.ApplicationInsights.AspNetCore」バージョン=「2.0.0-beta1の」クラススタートアップで
2.方法の設定は、私はapp.UseApplicationInsightsRequestTelemetry()を追加しませんでした。私はあなたがVS2017で「新しい」asp.netのコアを使用している場合は、すべての要求

+0

私は、これを追加するための 'Configure'メソッドではなく、' Program.cs'メソッドを使うはずの場所を読んでいます。私は両方でそれらを持っていて、テレメトリーを2倍にしていました。 – nhwilly

関連する問題