1

I have dotnet core web application hosted in Azure and using application insights for logging. I found I have abnormal amount of n/a critical trace message, like around 11k in just 1 min. Anyone has encountered this issue in application insights? enter image description here

library:

  • Microsoft.ApplicationInsights.AspNetCore Version=2.15.0
  • Microsoft.ApplicationInsights.DependencyCollector Version=2.15.0
  • Microsoft.ApplicationInsights.DiagnosticSourceListener Version=2.15.0
  • Microsoft.ApplicationInsights.EventSourceListener Version=2.15.0
  • Microsoft.ApplicationInsights.TraceListener Version=2.15.0
  • Microsoft.ApplicationInsights.WindowsServer Version=2.15.0
  • Microsoft.AspNetCore.Hosting Version=2.2.7

dotnet runtime: netcoreapp3.1

I have configured application insights at below.

public static IWebHostBuilder UseLoggingWithAppInsights(this IWebHostBuilder builder)
{
    builder.ConfigureServices((context, services) =>
    {
        services.AddApplicationInsightsTelemetry(applicationInsightsInstrumentKey);
    });

    var provider = services.BuildServiceProvider();
    IWebHostEnvironment host = provider.GetService<IWebHostEnvironment>();
    IHttpContextAccessor context = provider.GetService<IHttpContextAccessor>();

    TelemetryConfiguration.Active.EnableTelemetry(applicationInsightsInstrumentKey, appName, telemetryDetails, telemetrySettings, context, host);

    TelemetryConfiguration config = provider.GetService<TelemetryConfiguration>() ?? TelemetryConfiguration.CreateDefault();

    TelemetryConfiguration.Active.FireTraceToSeeIfActive();
    config.FireTraceToSeeIfActive();

    builder.ConfigureServices((context, services) =>
    {
        var instrumentationKey = context.Configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
        
        if (string.IsNullOrWhiteSpace(TelemetryConfiguration.Active.InstrumentationKey)
            || TelemetryConfiguration.Active.InstrumentationKey != instrumentationKey)
        {
            TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
        }

        var provider = services.BuildServiceProvider();
        Util.StaticApplicationLogging.LoggerFactory = provider.GetRequiredService<ILoggerFactory>();
    });

    builder.ConfigureLogging((builderContext, loggingBuilder) =>
    {
        loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));
    });

    return builder;
}
  • Please share your `appsettings.jsn` file. – Harshitha Mar 30 '23 at 05:21
  • @Harshitha which setting property do you want to look into? if appinsight, ApplicationInsightsBase: InstrumentationKey: ${EnvironmentValues:ApplicationInsights:InstrumentationKey} – endlesscalm Mar 30 '23 at 06:20
  • What does your Logging => LogLevel setting have ? – Harshitha Mar 30 '23 at 06:22
  • Refer this [SOThread](https://stackoverflow.com/questions/73534404/logs-not-getting-written-to-applicationinsights/73566848#73566848) to configure ApplicationInsights in .NET Core. – Harshitha Mar 30 '23 at 06:26
  • Using 'TelemetryConfiguration.Active` is not recommended in .NET Core. – Harshitha Mar 30 '23 at 06:28
  • @Harshitha Serilog: Properties: Application: ${ApplicationData:ApplicationContainerName} MinimumLevel: Default: Information Override: Microsoft: Warning System: Warning Enrich: - FromLogContext – endlesscalm Mar 30 '23 at 06:29
  • What exactly you want to log ? – Harshitha Mar 30 '23 at 06:32
  • @Harshitha currently it is logging error, each API call information, queue msg, etc. Just that n/a trace message i do not know what it is for and where was it coming from, and it is taking huge amount of space. – endlesscalm Mar 30 '23 at 06:51
  • `{ "Logging": { "ApplicationInsights": { "LogLevel": { "Default": "Debug", "Microsoft": "Error" } }, "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ApplicationInsights": { //"InstrmentationKey": "YourInstrumentationKey", "ConnectionString": "InstrumentationKey=a23d17fa-8c1e-40b1-918e-14d8915946d4;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/" } }` – Harshitha Mar 30 '23 at 07:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252868/discussion-between-harshitha-and-endlesscalm). – Harshitha Mar 30 '23 at 07:01

1 Answers1

0

Lots of n/a critical trace message in Azure application insights

The reason for this issue can be the incorrect configuration in appsettings.json file.

And the code which you have used is not recommended in .NET Core Applications.

 TelemetryConfiguration.Active.EnableTelemetry(applicationInsightsInstrumentKey, appName, telemetryDetails, telemetrySettings, context, host);

enter image description here

  • Reduce the severity level of the messages, this can be done by setting only the log levels which are required.

My Initial appsettings.json file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "AppInsightsConnString"
  }
}
  • With the above appsettings only Error,Critical and Warning messages are shown.
  • To Log Information ,Debug and Exception messages few changes have to be done in appsettings.json file.

appsettings.json file:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": { 
    "ConnectionString": "InstrumentationKey=a******;IngestionEndpoint=https://*******8.in.applicationinsights.azure.com/;LiveEndpoint=https://*******.livediagnostics.monitor.azure.com/"
  }
}
  • Make sure all the packages are latest and updated.

.csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.DiagnosticSourceListener" Version="2.15.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.EventSourceListener" Version="2.15.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.TraceListener" Version="2.15.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.21.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
  </ItemGroup>
</Project>

Startup.cs file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

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

        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
           services.AddApplicationInsightsTelemetry();
            services.AddControllersWithViews();
        }
       
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");          
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

Application Insights Traces:

enter image description here

enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9
  • without touching existing configuration and codes, is it possible to just filter out those critical n/a trace message? – endlesscalm Mar 31 '23 at 05:21