Thom
Thom

Reputation: 2493

Cannot connect ASP.NET Core web app with Application Insights

I have ASP.NET Core MVC project (one of these new-fangled things that starts up as a console app) in Visual Studio Community 2015 Update 3 hosted on Azure App Services. I'm trying to set up Application Insights, mostly because I'd like to capture diagnostic traces with the ApplicationInsightsTraceListener but the other features are useful too. I have yet to stumble upon the correct configuration to get anything at all to show up in the Azure Portal.

I added Application Insights support to my project using the Visual Studio extension. However, and this seems to be where things start going wrong, it does not create an ApplicationInsights.config. It adds some DLLs, and puts the InstrumentationKey in my app settings file, and I have manually added some more assemblies via NuGet, including Microsoft.ApplicationInsights.Web and Microsoft.ApplicationInsights.TraceListener. Publishing at this point results in no metrics being enabled in the Azure Portal, and the Live Stream telling me Not available: your app is offline or using an older SDK.

I have tried both 2.1.0 and a 2.2.0 beta. I have tried deleting all the Application Insights stuff, reinstalling the extension and starting again. I have also tried to skip using the extension, manually adding Microsoft.ApplicationInsights.Web et al, which some google results implied should create the config file also, but with no luck. I have downloaded other people's ApplicationInsights.config files from the web and attempted to make them sensible for my environment. At no point has anything ever worked in the Application Insights part of the portal.

What extra steps should the extension have taken in addition to creating a config file? Do I need to add code to my Startup.cs? Most of the documentation just seems to take for granted that the initial setup works.

Upvotes: 1

Views: 3290

Answers (3)

Matias Quaranta
Matias Quaranta

Reputation: 15603

You need to modify your Startup.cs, considering that your InstrumentationKey is on the appsettings.json:

public void ConfigureServices(IServiceCollection services)
{
    /*Simple Mvc web app*/
    services.AddMvc();

    services.AddApplicationInsightsTelemetry(Configuration);
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
    /*This should be before UseMvc*/
    app.UseApplicationInsightsRequestTelemetry();
    app.UseApplicationInsightsExceptionTelemetry();
    app.UseMvc();


}

It's important that the app.UseApplicationInsightsRequestTelemetry() is before app.UseMvc().

Upvotes: 3

Akodo_Shado
Akodo_Shado

Reputation: 860

UseApplicationInsightsExceptionTelemetry should be before UseMvc. If I used it after UseMvc it didn't save Exceptions, I saw only Requests. Of course you should also add nuget packages "Microsoft.ApplicationInsights.AspNetCore" and add to the appsettings.json:

      "ApplicationInsights": {
    "InstrumentationKey": "xxxxxxx-xxxxx-xxxx-xxx-xxxxxxxxxx"
  }

My Startup.cs looks that:

    public Startup(IHostingEnvironment env)
    {
        ...
        if (env.IsDevelopment())
        {
            builder.AddApplicationInsightsSettings(developerMode: true);
        }
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddMvc();
        ....
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();
    }

Upvotes: 0

Dmitry Matveev
Dmitry Matveev

Reputation: 2679

There is a separate AI SDK for ASP.NET Core projects, you can find the project on GitHub(ApplicationInsights-aspnetcore) and the Nuget package on Nuget.org (Microsoft.ApplicationInsights.AspNetCore)

To start the collection, AI middleware should be wired into the project code (once you add the appropriate Nuget package), namely Request and Exception collection middleware. An additional step to build AI Configuration should be added to the startup.

Getting Started on GitHub project should cover this, but I found it empty today, however, Configure experience mentions how to add InstrumentationKey and some extra collection, e.g. for configuration based approach to set IKey:

If you are using json configuration provider - add the following into config.json

    "ApplicationInsights": {
    "InstrumentationKey": "11111111-2222-3333-4444-555555555555"
}

If you are using environment variables:

SET ApplicationInsights:InstrumentationKey=11111111-2222-3333-4444-555555555555

note: Environment variable that is set by azure website (APPINSIGHTS_INSTRUMENTATIONKEY) is not supported.

Or any other configuration provider format you defined in your application (configuration setting name is ApplicationInsights:InstrumentationKey) before the call to add insights telemetry in Startup.cs:

services.AddApplicationInsightsTelemetry(Configuration);

Upvotes: 2

Related Questions