3

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.

0

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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".

2 Comments

I've edited my question. The log entries are not from my controller.
There's no way to do that.
0

I hope this help
A filter certainly works but I would prefer to turn off the logger (or logger hierarchy) directly like this:

<logger name="YourNameSpace.WithNoLogging" additivity="false">
  <level value="OFF" />        
</logger>
<logger name="MyClass" additivity="false">
  <level value="OFF" />        
</logger>
<root>
   <level value="ALL" />
<appender-ref ref="YourAppender" />

The second "example" turns off logging for your class (according to your question).

1 Comment

I've edited my question. The log entries are not from my controller.
0

There is a way to do it with nlog but may be possible with log4net. It does require custom code on your part.

Create a middleware that assigns an activity id to the Trace.CorrelationManager.ActivityId. Also, record the activity id in a HttpContext item if the request path is from your health monitor.

In your logging config, you will need a package similar to NLog.Web.AspNetCore to get access to both activity ids so as to use a logging filter:

In the code below, my context item is "ActivityIdToSink"

<logger name="Microsoft.AspNetCore.HttpLogging.*" minLevel="Trace"  final="true" writeTo="allFile">
  <filters defaultAction="Neutral">
    <when condition="equals('${aspnet-item:variable=ActivityIdToSink}', '${activityId}')" action="Ignore"  />
  </filters>
</logger>  

This will cause the health probe to not be written to the allFile log.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.