Search code examples
azure-web-app-serviceazure-application-insightsazure-webapps

Add requests to application insights from ASP.NET CORE WEB API


I have an API project I have been running for some time in an Azure Web App. However I do not see any requests in the requests table in application insights. Am I missing something?

Startup.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using VendorFormAPI.Authentication;
using VendorFormAPI.Extensions;
using VendorFormData.Repositories;


namespace VendorFormAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry();            
            var dbConnStr = Configuration["Values:DBConnection"];

            services.AddTransient<VendorFormRepo, VendorFormRepo>((ctx) =>
            {

                var vfRepo = new VendorFormRepo(dbConnStr);
                return vfRepo;
            });

            services.AddTransient<FilesRepo, FilesRepo>((ctx) =>
            {
                var strgConnStr = Configuration["Values:StorageConnection"];
                var vfFilesRepo = new FilesRepo(strgConnStr, dbConnStr);
                return vfFilesRepo;
            });

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                    {
                        builder.WithOrigins("http://localhost:3000").AllowAnyHeader().AllowAnyMethod();
                    });
            });

            services.AddControllers();
            services.AddAuthentication(o => o.AddScheme("ApiKey", a => a.HandlerType = typeof(ApiKeyAuthenticationHandler)));         
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.ConfigureExceptionHandler();

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseCors(); // second

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

appsettings.cs:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Trace",
        "System": "None",
        "Microsoft": "None"
      }
    }
  },
  "ApplicationInsights": {
    "Instrumentationkey": "xx-xx-4687-b1fc-xx"
  },
  "AllowedHosts": "*",
  "Values": {      
        }
}

Configuration in Azure: This has been set: APPINSIGHTS_INSTRUMENTATIONKEY=xxx


Solution

  • There are many reasons if nothing is sent to Application Insights instance:

    1.Please check if the INSTRUMENTATIONKEY is correct.

    2.Sometimes, people have more than 1 Application Insights. They may send data using INSTRUMENTATIONKEY for Application_Insights_1, but check the results in Application_Insights_2.

    3.Update the packages of Application Insights to the latest one, if there is no special requirements.

    4.Check your code to see if there're some filter logic which is used to filter the logs.

    5.Log level is not set correctly, like set to None etc.

    6.Check if the sampling is enabled in azure portal or in your code.

    7.Check the Daily cap settings of your application insights. If it hits the limitation, the data may be abandoned.

    And here're some suggestions:

    1.You'd better run the project locally. For example, you may run it in visual studio, and check the output in visual studio output window. If the data is sent, you can see them in the output window with correct INSTRUMENTATIONKEY.

    2.If it's deployed to azure, you can see the data in Live metrics immediately.