Why am I Only Seeing Warning Level Logs in Application Insights?

Neeraj Mahajan 81 Reputation points
2024-12-19T16:21:16.08+00:00

I am working on an ASP.NET Core application and have integrated Application Insights for logging. However, I'm facing an issue where only Warning level logs (severity level 2) are being sent to Application Insights, even though I expect to see logs of Information level (severity level 1) and above.

I am using Microsoft.ApplicationInsights.AspNetCore package.

Configuration Details:

  • In my Program.cs, I am loading configuration from Key Vault and environment variables:
Dictionary
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,396 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,007 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,152 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ketsha 250 Reputation points Microsoft Employee
    2024-12-19T18:19:56.17+00:00

    Hi Neeraj - Thanks for resending the question.

    Here are the steps and configurations you can check to ensure that Information level logs are captured:

    Verify Logging Configuration: Ensure that your logging configuration in appsettings.json or environment variables is correctly set to capture Information level logs. Here is an example of how your appsettings.json might look:

    {
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Information"
          }
        },
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      }
    }
    

    Configure Application Insights Logging: Make sure you have added the Application Insights logging provider correctly in your Program.cs file. Here is an example:

    using Microsoft.Extensions.Logging.ApplicationInsights;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddApplicationInsightsTelemetry();
    
    // Configure logging
    builder.Logging.AddApplicationInsights(
        configureTelemetryConfiguration: (config) => config.ConnectionString = builder.Configuration.GetConnectionString("ApplicationInsights"),
        configureApplicationInsightsLoggerOptions: (options) => { }
    );
    
    builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapRazorPages();
    app.Run();
    

    Check Environment Variables: Ensure that there are no environment variables overriding your logging configuration. You can print out the current log level to verify:

    var logLevel = builder.Configuration["Logging:ApplicationInsights:LogLevel:Default"];
    Console.WriteLine($"Current LogLevel from config: {logLevel}");
    

    Update NuGet Packages: Ensure that you are using the latest version of the Microsoft.ApplicationInsights.AspNetCore package. Sometimes, issues can arise from using outdated packages.

    Custom Telemetry Processor: If you have a custom telemetry processor or filter, ensure that it is not filtering out Information level logs.

    By following these steps, you should be able to ensure that Information level logs are captured and sent to Application Insights. If the issue persists, you might want to check the official documentation for any additional configuration details or updates


2 additional answers

Sort by: Most helpful
  1. Ketsha 250 Reputation points Microsoft Employee
    2024-12-19T16:55:24.32+00:00

    @Neeraj Mahajan -

    Here are a few steps to troubleshoot and resolve this issue:

    1. Verify Log Level Configuration

    Ensure that your log level configuration is correctly set in your appsettings.json or environment variables. For example:

    JSON

    {
    
    1. Check Application Insights Configuration

    Make sure that Application Insights is configured to capture the desired log levels. You can do this in your Program.cs:

    C#

    builder.Services.AddApplicationInsightsTelemetry(options =>
    {
        options.EnableAdaptiveSampling = 
    
    1. Verify Telemetry Initializers

    Ensure that there are no telemetry initializers that might be filtering out Information level logs. Check your TelemetryConfiguration for any custom initializers.

    1. Console Logs Verification

    Since you mentioned that Information level logs appear in the console, double-check that the Application Insights SDK is correctly capturing these logs. You can add a custom telemetry processor to inspect the logs being sent to Application Insights.

    1. Adaptive Sampling

    Adaptive sampling might be filtering out lower-severity logs. You can disable it or adjust the sampling settings:

    C#

    builder.Services.Configure<TelemetryConfiguration>((config) =>
    {
        
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.