azureasp.net-coreazure-web-app-serviceasp.net-core-webapiazure-application-insights

Application Insights does not capture information level logging


I have a simple Asp.Net Core Web API application. For this example I have followed the official documentation here: https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.18.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

appsettings.json

{
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Information"
    },
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "AllowedHosts": "*"
}

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiMonitoring", Version = "v1" });
            });

            services.AddApplicationInsightsTelemetry();
        }

Controller.cs

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var now = DateTime.Now;

            _logger.LogTrace("--- {time}: Logging LogTrace. Value {value}", now, 0);
            _logger.LogDebug("--- {time}: Logging LogDebug. Value {value}", now, 1);
            _logger.LogInformation("--- {time}: Logging LogInformation. Value {value}", now, 2);
            _logger.LogWarning("--- {time}: Logging LogWarning. Value {value}", now, 3);
            _logger.LogError("--- {time}: Logging LogError. Value {value}", now, 4);
            _logger.LogCritical("--- {time}: Logging LogCritical. Value {value}", now, 5);

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

When running the application locally I can see all the logs levels as expected.

enter image description here

But Application Insights for the same request is only capturing warning level logs and up. This following image if from Live Metrics in Application Insights.

enter image description here

The same goes when querying logs in Application Insights.

enter image description here

What else it is needed for an application to log information level and be captured by Application Insights?


Solution

  • Your config is wrong. The LogLevel needs to be specified under the Logging section

    {
      "ApplicationInsights": {
        "LogLevel": {
          "Default": "Information"
        },
        "InstrumentationKey": "my-instrumentation-key",
        "EnableAdaptiveSampling": false,
        "EnablePerformanceCounterCollectionModule": false
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information"
        }
      },
      "AllowedHosts": "*"
    }
    

    A valid config would be

    {
      "ApplicationInsights": {
        "InstrumentationKey": "my-instrumentation-key",
        "EnableAdaptiveSampling": false,
        "EnablePerformanceCounterCollectionModule": false
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        },
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Information"
          }
        }
      }
    }
    

    See the docs