harpal
harpal

Reputation: 476

dotnet core logging showing info result always

I am learning .net core logging. I have read some blogs and docs also. I am wondering why it's showing always "Microsoft.Hosting.Lifetime" Information logs every time with following settings.

{
  "Logging": {
    "LogLevel": {
      "Default": "Error",
      "Microsoft": "Error",
      "Microsoft.Hosting.Lifetime": "Error",
      "DemoWeb.Controllers.HomeController":  "Error"
    }
  },
  "AllowedHosts": "*"
}

enter image description here

To double check(If my appsettings.json is working) I added this in ConfigureService.

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(config =>
    {
        // clear out default configuration
        config.ClearProviders();

        config.AddConfiguration(Configuration.GetSection("Logging"));
        config.AddDebug();
        config.AddEventSourceLogger();
        config.AddConsole();                
    });
    services.AddControllersWithViews();
}

It's working as expected for my own added "HomeController" Class.

I just want to remove "Microsoft.Hosting.Lifetime" from console logs.

Thanks in advance

Upvotes: 1

Views: 1314

Answers (1)

Fei Han
Fei Han

Reputation: 27793

Firstly, as @KirkLarkin mentioned, in development, appsettings.Development.json configuration would overwrite values found in appsettings.json.

For more information, you can check : https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#appsettingsjson

want to remove "Microsoft.Hosting.Lifetime" from console logs

In your project, multiple logging providers can be enabled. If you just want to turn off Information level logs about "Microsoft.Hosting.Lifetime" from ConsoleLogger, you can try to apply log filter rule(s) as below.

services.AddLogging(config =>
{
    // clear out default configuration
    config.ClearProviders();

    config.AddConfiguration(Configuration.GetSection("Logging"));

    //add log filter for ConsoleLoggerProvider
    config.AddFilter<ConsoleLoggerProvider>("Microsoft.Hosting.Lifetime", LogLevel.Error);

    config.AddDebug();
    config.AddEventSourceLogger();
    config.AddConsole();
});

And you can achieve same by configuring logging for that specific provider.

"Console": {
  "IncludeScopes": true,
  "LogLevel": {
    "Microsoft.Hosting.Lifetime": "Error"
  }
}
 

Upvotes: 2

Related Questions