Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Application Insight with ASP API Core

Community

I'm having troubles connecting Application Insights to my ASP WEB API Core. Following standard manuals I still cannot find any record in my AppInsights account. I used a lot of manuals, but the are pretty identical and describe how to configure App Insights for ASP Core (not API Core). So I'm wondering is some special configuration (or nuget package or whatever) is required to make AppInsights track requests to my API service?

Once I cannot make AppInsights work out of box, I still can create instance of TelemetryClient and publish data manually, but this is not desirable in my case.

Important note: I'm using VS 2017 RC, Web APi Core project (csproj)

UPD

csproj file content:

  <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();
        }
like image 985
Valentine Avatar asked Feb 28 '17 15:02

Valentine


People also ask

How do you implement application Insights in web API?

Add Application Insights automatically From within your ASP.NET web app project in Visual Studio: Select Project > Add Application Insights Telemetry > Application Insights Sdk (local) > Next > Finish > Close.

Can I use application Insights with JavaScript?

Application Insights can be used with any webpages by adding a short piece of JavaScript. Node. js has a standalone SDK. If your web service is Java or ASP.NET, you can use the server-side SDKs with the client-side JavaScript SDK to get an end-to-end understanding of your app's performance.


1 Answers

If you're using the "new" asp.net core in VS2017, then the old instructions are wrong, as they are for the previous xproj based asp.net core implementations.

If you create a new asp.net core web project in VS2017, ApplicationInsights will be already installed from the start, 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 asp.net core team has updated them at all)

Those projects will already have Application Insights wired up as well, not in Startup.cs (that's 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, like:

 @inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

at the top, and

 @Html.Raw(JavaScriptSnippet.FullScript)

inside the bottom of the <head> tag.

if you're migrating from a previous version of asp.net core and app insights, you'll also have to remove things like:

@Html.ApplicationInsightsJavaScript(TelemetryConfiguration) 

from _Layout.cshtml and replace them with the lines above, and you can remove all of the lines like:

app.UseApplicationInsightsExceptionTelemetry();

in Startup.cs (if you're using 2.x versions of the packages, i believe those items will all show deprecation warnings as well, as they're no longer needed)

VS2017's official release notes include this information as a section in the "known issues" for application insights

like image 135
John Gardner Avatar answered Oct 24 '22 11:10

John Gardner