Search code examples
c#asp.netazureazure-application-insights

Not all log levels are being logged in Application Insights


I have an application set up to use Application Insights, and I'm trying to manually log some custom information. Here's my controller:

public class MyController : Controller
{
    private ILogger<MyController> Logger { get; set; }

    public MyController(ILogger<MyController> logger)
    {            
        Logger = logger;
    }

    public IActionResult Index()
    {
        Logger.LogCritical("Test critical");
        Logger.LogError("Test error");
        Logger.LogWarning("Test warning");
        Logger.LogInformation("Test information");
        Logger.LogDebug("Test debug");
        Logger.LogTrace("Test trace");

        ...
    }

I have this in my Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
    ...

}

And this in my Program.cs:

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .UseStartup<Startup>()
            .Build();

And in my appsettings.json:

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}

When I look in Application Insights in my Azure portal, the only things logged are:

enter image description here

So it's skipping a few for some reason, and only logging Critical, Warning, and Error. I'd mainly like to use LogInformation.

Is there something I need to change in my Logging settings or maybe in the Startup file?


Solution

  • If you want to collect all telemetry data, you should not specify Warning logLevel in appsettings.json. The Warning log level will only collect the Warning / Error / Critical data, but abandon the Trace / Debug / Information data.

    For more details, please refer to this doc and this doc.

    Please specify the log level of application insights as Trace in appsettings.json, like below:

    "Logging": {
      "IncludeScopes": false,
      "ApplicationInsights": {
        "LogLevel": {
          "Default": "Trace"
        }
      },
      "LogLevel": {
        "Default": "Warning"
      }
    }