4

I have an ASP.NET Core 2.2 app. I want to use NLog in this app. While I have successfully integrated NLog, there are too many logs being written. Specifically, I see a ton of messages from Microsoft.AspNetCore.. I really only want to show messages that comes from my code.

I copied the nlog.config file shows here. However, I still see a ton of Microsoft.AspNetCore messages. How do I prevent those messages from getting written to the logs? What am I missing?

Learner
  • 43
  • 2
  • 6

3 Answers3

5

You can't stop them, but you can filter them out below a certain log level. For example, you can add something like the following to appsettings.json:

"Logging": {
    "LogLevel": {
        "Microsoft.AspNetCore": "Critical"
    }
}

That will then only log critical messages for any namespace beginning with Microsoft.AspNetCore. Any lower log levels will be effectively ignored.

However, I wouldn't recommend actually doing this, or at least going this extreme. If there's a particular message you're wanting to exclude, target that namespace exactly, and then then set the minimum log level to something more reasonable like Warning. Most of the log chatter is Informational level, so that will get excluded, then, but you'll still get warnings and higher, in case something is actually wrong.

For more on filtering, see the docs.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
5

If you are using the NLog.config from the wiki-page:

<logger name="Microsoft.*" maxlevel="Info" final="true" />  <!-- Blackhole -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />

Then you can change it into this (Changing maxLevel into Error)

<logger name="Microsoft.*" maxlevel="Error" final="true" />  <!-- Blackhole -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />

You will find your output in the target output ownFile-web

See also: https://github.com/nlog/nlog/wiki/Configuration-file#rules

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Unfortunately, that did not seem to work. I still see all of the `Microsoft.*` entries. – Learner Sep 23 '19 at 17:06
  • @Learner Either you are looking in the wrong log-file, or you didn't deploy the update nlog.config. Try and update the original question with your current NLog.config – Rolf Kristensen Sep 23 '19 at 18:10
  • @Learner Maybe also include an example of a Log-message that you feel is unwanted. Please make sure `${logger}` is included in the NLog-layout. – Rolf Kristensen Sep 23 '19 at 18:17
  • I think above will only stop writing Microsoft.* logger to log in `ownFile-web` file. But to prevent it from `allfile`, we need to write the same rule above `allfile` rule as well. – Ankush Jain Apr 16 '20 at 13:40
0

There may be different causes for this. If you have multiple configuration files (for example appsettings.json and appsettings.Development.json, then you need to make sure that the rules you set in appsettings.Development.json correctly override the rules set in appsettings.json. Specifically, rules should be named and ordered and the catch all rule should be ordered to appear last.

appsettings.json:

  "NLog": {
    "rules": {
      "999": {
        "logger": "*",
        "minLevel": "Debug",
        "writeTo": "console"
      }
    }
  }

appsettings.Development.json:

  "NLog": {
    "rules": {
      "0_Microsoft_AspNetCore": {
        "logger": "Microsoft.AspNetCore.*",
        "maxLevel": "Error",
        "final": true
      },
      "999": {
        "logger": "*",
        "minLevel": "Debug",
        "writeTo": "console"
      }
    }
  }

See also: https://github.com/NLog/NLog.Extensions.Logging/issues/472 and https://stackoverflow.com/a/7995960/474034

lanoxx
  • 12,249
  • 13
  • 87
  • 142