How to Enable and Disable Logs in ASP.NET Core 6

Hello… Happy New Year for all you, guys! May God give strength to you and bless you to achieve whatever you want in your life.

Almost all the .NET 6 (and previous) templates have enabled by default the new logging system. This is very useful but in a big project, the logs might be too much confusing. But this logging system is not an on/off system. You can decide, from the settings, the tracing level for all the libraries and namespaces in the project.

By default, your appsettings.json looks more or less like this one:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },

Customize the logging level by the source it’s very easy. You can just add the namespace and the minimum log level:
This is one of my appsettings.json for a real project:

"Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information",
      "Microsoft.AspNetCore.Authentication": "Warning",
      "Microsoft.IdentityModel": "Warning"
    }
  },

LogLevel indicates the severity of the log and ranges from 0 to 6:

Trace = 0, Debug = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, and None = 6

When a LogLevel is specified, logging is enabled for messages at the specified level and higher.

Happy Coding!

Thanks for reading this post and we will back with other interesting tutorial.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *