Search code examples
azureasp.net-coreazure-application-insights

Application Insights -_logger.LogInformation


I have created a very simple asp.net core application to learn more about Azure Application Insights

I can log LogWarning, LogError and LogCritical messages to my Azure Application Insights which I see when I query "traces" table under logs. The issue I am having for LogDebug, LogInformation and LogTrace I cannot find the messages

Below is some related code/config:

builder.Services.AddApplicationInsightsTelemetry();



// Example log level configuration in Startup.cs

builder.Services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));



"Logging": {

"LogLevel": {

"Default": "Information",

//Only the WARNING messages for HttpClient will be logged

"System.Net.Http.HttpClient": "Warning"

}

}

Appreciate any help


Solution

  • The issue I am having for LogDebug, LogInformation and LogTrace I cannot find the messages

    I have created simple dotnet core web application with .net version 6.0 in visual studio. I was configure application insights in the application by using appinsights connection string.

    .cs proj:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
        <ItemGroup>
            <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
        </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.22.0" />
      </ItemGroup>
    </Project>
    

    program.cs:

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddControllersWithViews();
    builder.Services.AddApplicationInsightsTelemetry();
    //var abc = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
    var app = builder.Build();
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.Run();
    

    appsettings.json:

    {
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Debug",
            "Microsoft": "Error"
          }
        },
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "ApplicationInsights": {
        "ConnectionString": "Your-application insights-connection string"
      }
    }
    

    HomeController.cs:

    using appinsightweb.Models;
    using Microsoft.AspNetCore.Mvc;
    using System.Diagnostics;
    
    namespace appinsightweb.Controllers
    {
        public class HomeController : Controller
        {
            private readonly ILogger<HomeController> _logger;
    
            public HomeController(ILogger<HomeController> logger)
            {
                _logger = logger;
            }
    
            public IActionResult Index()
            {
                _logger.LogInformation("This is an Information log.");
                _logger.LogWarning("This is a Warning log.");
                _logger.LogError("This is an Error log.");
                _logger.LogCritical("This is a Critical log.");
    
                // For Debug and Trace logs, ensure that the log level is configured appropriately in Startup.cs
    
                _logger.LogDebug("This is a Debug log.");
                _logger.LogTrace("This is a Trace log.");
                return View();
            }
    

    By using above code and configuration able to get the different type of logs in application insights. Check below:

    enter image description here