Use Application Insight with ASP API

Main

I'm having problems connecting Application Insights to my ASP WEB API. Following the standard guides, I still cannot find the entry in my AppInsights account. I used a lot of guides, but they are pretty identical and describe how to configure App Insights for ASP Core (not API Core). So I'm wondering if I need some special configuration (or nuget package or something else) to get AppInsights to track requests to my API service?

As soon as I cannot get AppInsights to work out of the box, I can still create an instance of TelemetryClient and publish the data manually, but in my case this is not desirable.

Important Note: I am using VS 2017 RC, Web APi Core project (csproj)

UPD

Csproj file contents:

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

Configuration in 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(); } 
+7
asp.net-web-api asp.net-core azure-application-insights
source share
2 answers

If you are using the β€œnew” asp.net kernel in VS2017, then the old instructions are incorrect, as with previous xproj-based asp.net kernel implementations.

If you are creating a new asp.net web project in VS2017, ApplicationInsights will already be installed from the very beginning and should be versions:

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

(or newer if the main asp.net team has updated them at all)

These projects will already be connected to Application Insights, not in Startup.cs (this is the old way), but in Program.cs:

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

and possibly in web templates, for example:

  @inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet 

above and

  @Html.Raw(JavaScriptSnippet.FullScript) 

at the bottom of the <head> .

if you upgrade from a previous version of the asp.net kernel and applications, you will also have to uninstall things like:

 @Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

from _Layout.cshtml and replace them with the lines above, and you can delete all lines, for example:

app.UseApplicationInsightsExceptionTelemetry ();

in Startup.cs (if you are using version 2.x packages, I believe that these items will also display obsolete warnings, as they are no longer needed)

VS2017 official release notes include this information as a section in the β€œknown issues” for understanding applications

+12
source share

The problem is resolved thanks to this article

In my configuration, I missed 2 points:
1. The package "Microsoft.ApplicationInsights.AspNetCore" Version = "2.0.0-beta1" must be specified in csproj
2. In the Startup class, the Configure method is omitted to add app.UseApplicationInsightsRequestTelemetry ();

When I changed what is described above, AppInsights began to track all requests

0
source share

All Articles