Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude specific controller from logging in asp.net core

There is a HealthCheck controller with GET resource in our ASP.net core app that simply returns 200 - OK. This resource is called every 5 seconds which spams our log files. We are using Log4net.

How can I exclude specific controller/resource from logging?

EDIT: The log entries come from Microsoft.AspNetCore.* namespace, they are the informations about request starting/finishing/route matching/etc. I'd like to turn these off for single controller only.

like image 340
horato Avatar asked Mar 24 '26 10:03

horato


2 Answers

I've found a way using WebHostBuilder.ConfigureLogging.

.ConfigureLogging(x => x.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning))

Sadly you have to hard-code this, there seems to be no way to do this from the config.

like image 142
horato Avatar answered Mar 26 '26 22:03

horato


You specify that you're using Log4Net, but I'm assuming you're actually using the Log4Net provider with Microsoft.Extensions.Logging. If that is in fact the case, you can filter the logs via config. Something along the lines of:

{
  "Logging": {
    ...
    "LogLevel": {
      "Namespace.To.MyController": "Error",
      "Default": "Debug"
    }
  }
}

That will then only log in that namespace, if the level of the message is Error or higher, filtering out things like Information level logs, which is what you're likely referring to as the "spam".

like image 43
Chris Pratt Avatar answered Mar 26 '26 22:03

Chris Pratt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!