DEV Community

Cover image for Enabling and Disabling Logs in ASP.NET Core 6
Emanuele Bartolesi
Emanuele Bartolesi

Posted on

Enabling and Disabling Logs in ASP.NET Core 6

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"
    }
  },
Enter fullscreen mode Exit fullscreen mode

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"
    }
  },
Enter fullscreen mode Exit fullscreen mode

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 Logging!


Thanks for reading this post, I hope you found it interesting!

Feel free to follow me to get notified when new articles are out 🙂

Top comments (0)