Application Insights does not collect ILogger error logs

152 views Asked by At

I am using App Insights. It is configured for my app service and it tracks failures, but unfortunately I can't see my ILogger.LogError logs. LoggingLevel is set to Warning.

builder.Services.AddApplicationInsightsTelemetry();

I read that App Insights should collect logs if log level set correctly but something is wrong

1

There are 1 answers

3
RithwikBojja On

I have referred the code from this SO-Thread's answer and modified a bit to reproduce your issue like below:

csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <ApplicationInsightsResourceId>/subscriptions/b83c1ed3/resourceGroups/bojja/providers/microsoft.insights/components/rithwik98989</ApplicationInsightsResourceId>
        <UserSecretsId>8ff9a4a2-2cf0-4659-a5cb-168ff7ca8634</UserSecretsId>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.4.0" />
    </ItemGroup>
</Project>

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=01dfb043-2a81-0e41"
  }
}

Controller.cs:

using Microsoft.AspNetCore.Mvc;

namespace WebApplication35.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }


        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            var errorMessage = "Hello Rithwik, This is error";
            _logger.LogError(errorMessage);

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

Program.cs:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(typeof(ITelemetryChannel), new ServerTelemetryChannel());
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddServiceProfiler();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Output:

enter image description here

enter image description here