ApplicationInsights is not considering json configured log level

Viewed 1231

Given this appsettings.json

{
  "ApplicationInsights": {
    "InstrumentationKey": "foobar",
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "AllowedHosts": "*"
}

And this from startup.cs

public void ConfigureServices(IServiceCollection services)
{
    var appSettingsSection = Configuration.GetSection("TD");
    var appSettings = new AppSettings();
    new ConfigureFromConfigurationOptions<AppSettings>(appSettingsSection).Configure(appSettings);
    services.Configure<AppSettings>(appSettingsSection);

    services.AddApplicationInsightsTelemetry();

    services.AddTransient<IMyService, MyService>();
    services.AddControllersWithViews();
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp";
    });
}

Why am I still only seeing Critical, Error and Warning in my AI output?

e.g. from arbitrary logs from a controller

public async Task<IActionResult> MyAction(string foobar)
{
    Logger.LogDebug($"Ilogger debug {foobar}");
    Logger.LogInformation($"Ilogger info {foobar}");
    Logger.LogWarning($"Ilogger warning {foobar}");
    Logger.LogError($"Ilogger error {foobar}");
    Logger.LogCritical($"Ilogger critical {foobar}");
}

According to the microsoft docs here

ApplicationInsightsLoggerProvider is enabled by default in Microsoft.ApplicationInsights.AspNet SDK version 2.7.1 (and later) when you turn on regular Application Insights monitoring through either of the methods:

  • By calling the UseApplicationInsights extension method onIWebHostBuilder (Now obsolete)
  • By calling the AddApplicationInsightsTelemetry extension method on IServiceCollection

So I am a bit lost as to why the Debug/Info traces are not being output.

There are no overrides or logging settings in appsettings.development.json.

I am using AppInsights SDK 2.13.1.

Asp NET Core 3.1.

1 Answers

There is something wrong with the appsettings.json(if you're sure no other place overrides the logging level). The log level for application insights should be placed within the Logging element in the .json file(see the official doc here).

The correct one should look like this:

{
  "ApplicationInsights": {
    "InstrumentationKey": "xxxxx"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  },
  "AllowedHosts": "*"
}

And the test result:

enter image description here

Related