1

Although the NLog settings in appsettings.json should limit the Logger "Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler" to a finalMilLevel "Warn", I am still getting all the trace logs written in all my targets (file and console).

Tried to move the rule up and down in the rules list, tried to reduce the Logger name to only "Microsoft.*" and to raise the finalMinLevel to "Error" to avoid potential naming conflicts between Microsoft Logging and NLog, but with no luck.

The only workaround with partial success was to use the NLogProviderOptions solution:

    builder.AddNLog(new NLogProviderOptions()
    {
        RemoveLoggerFactoryFilter = false
    });

but I am losing some debug\trace logs from other loggers, and still I am not able to set the minimum level to Warn: just removed the trace\debug entries, I am still getting the unwanted Info level messages.

My NLog section of appsettings.json is

 "NLog": {
    "internalLogLevel": "Info",
    "internalLogFile": "internal-nlog.txt",
    "extensions": [
      { "assembly": "NLog.Extensions.Logging" }
    ],
    "variables": {
      "var_logdir": "C:\\test\\test1\\__Logs"
    },
    "targets": {
      "async": true,
      "logfile": {
        "type": "File",
        "fileName": "${var:var_logdir}/Dispatcher-${shortdate}.log",
        "layout": "${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${callsite}|${message:exceptionSeparator=|:withException=true}"
      },
      "applogfile": {
        "type": "File",
        "fileName": "${var:var_logdir}/Dispatcher-xx-${shortdate}.log",
        "layout": "${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${callsite}|${message:exceptionSeparator=|:withException=true}"
      },
      "logconsole": {
        "type": "ColoredConsole",
        "useDefaultRowHighlightingRules": true,
        "detectConsoleAvailable": true,
        "DetectOutputRedirected": true,
        "layout": "${longdate}|${level}|${callsite}|${message:exceptionSeparator=|:withException=true}"
      }
    },
    "rules": [
      {
        "logger": "Microsoft.*",
        "finalMinLevel": "Warn"
      },
      {
        "logger": "Microsoft.EntityFrameworkCore*",
        "finalMinLevel": "Info"
      },
      {
        "logger": "*",
        "minLevel": "Trace",
        "writeTo": "logconsole"
      },
      {
        "logger": "*",
        "minLevel": "Trace",
        "writeTo": "logfile"
      },
      {
        "logger": "CFX*",
        "minLevel": "Trace",
        "writeTo": "applogfile"
      },
      {
        "logger": "Order*",
        "minLevel": "Trace",
        "writeTo": "applogfile"
      },
      {
        "logger": "Trading*",
        "minLevel": "Trace",
        "writeTo": "applogfile"
      }
    ]
  },

The project is a .Net 7 Worker Service, with the latest packages\updates.

UPDATE:

Just a quick update to confirm that the suggestion of Rolf Kristensen completely solves my problem. Changed the layout to:

"layout": "${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${logger}|${message:exceptionSeparator=|:withException=true}"

and tested many combinations of Rules with success.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Alberto
  • 13
  • 3
  • I can see that you are using the expensive [${callsite}](https://github.com/NLog/NLog/wiki/Callsite-layout-renderer) in your Layout, which is different from `${logger}`. NLog Logging Rules performs filtering based on Logger-name. I recommend that you update the Layout to use `${logger}` (instead of `${callsite}`) and include sample of the unwanted target-ouput in your question. – Rolf Kristensen May 09 '23 at 16:04
  • I can confirm that the suggestion of @RolfKristensen completely solves my problem! After changing ${callsite} to ${logger} I tested various combinations of filters and everything is working perfectly. – Alberto May 10 '23 at 00:51

1 Answers1

0

I can see that you are using the expensive ${callsite} in your Layout, which is different from ${logger}.

The NLog Logging Rules performs the filtering based on Logger-name, so maybe it because you setup filtering to match ${callsite} which will not work.

I recommend that you update the target Layout to use ${logger} (instead of ${callsite}). If you still have unwanted target-output, then please update the question, so one can see the sample output from ${logger}.

See also: https://github.com/NLog/NLog/wiki/Logging-Rules-FinalMinLevel

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70