1

I have Asp.NET Core application that uses a third-party library that outputs messages with the INFO information level:

_logger.LogInformation("About information...");

This is about how the log display is configured (I use a JSON configuration):

{
  "NLog": {
    "targets": {
      "console": {
        "type": "Console",
        "layout": "${logger}|${date}|${level:uppercase=true}|${message}"
      }
    }
  }
}

ThirdPartyNamespace.Runner|2024/11/12 12:24:27.141|INFO|About information...

In the logs for the logging level, I logically check INFO. Is it possible to redefine the logging level for a specific logger without changing the code from INFO to DEBUG?

This rule doesn't work (I get an exception: Unhandled exception. NLog.NLogConfigurationException: 'ConditionBasedFilter' cannot assign unknown property 'level'='Debug'). Maybe there is a right working way?

{
  "rules": [
    {
      "logger": "ThirdPartyNamespace.Runner",
      "minLevel": "Debug",
      "writeTo": "Console",
      "filters": [
        {
          "type": "when",
          "condition": "level == LogLevel.Info",
          "action": "Log",
          "level": "Debug"
        }
      ]
    }
  ]
}
2
  • in my opinion there is no direct way to change the log level of log event from info to debug using the filter. You must set the log level. You can control the output by using the minLevel and maxLevel. here is the document you could refer to get more detail about what is the use of the filter and how it works github.com/NLog/NLog/wiki/Configuration-file#rules. i have tested some of the code but non if the code works for your requirement Commented Nov 12, 2024 at 14:15
  • Unfortunately, setting the min and max levels only filters which messages to show. A third-party library writes as an INFO level, which creates "noise" among my messages. I can't not show them. If it were possible to raise the level without changing the code (since I don't have access to the code), it would be great. Apparently, this is impossible. Commented Nov 12, 2024 at 14:50

3 Answers 3

0

You can set the log level for specific loggers directly in the NLog configuration (NLog.config) by specifying the minLevel and maxLevel properties.

Here’s an example for setting DEBUG level only for a specific logger:

<rules>
  <!-- Default rule for all loggers -->
  <logger name="*" minLevel="Info" writeTo="logfile" />
  
  <!-- Specific rule for the desired logger with DEBUG level -->
  <logger name="YourSpecificLoggerName" minLevel="Debug" writeTo="logfile" />
</rules>
Sign up to request clarification or add additional context in comments.

Comments

0

In my opinion there is no direct way to change the log level of log event from info to debug using the filter. You must set the log level. You can control the output by using the minLevel and maxLevel. The configuration settings, like minLevel and maxLevel, only allow filtering based on levels but do not modify the actual level of incoming logs. here is the document you could refer to get more detail about what the use of the filter is and how it works Configuration file.

As you don't have access to the code and logs are coming from the third-party library the only way is you can create the file for the third party logs by modify the appsetting.json file:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "NLog": {
        "targets": {
            "console": {
                "type": "Console",
                "layout": "${logger}|${date}|${level:uppercase=true}|${message}"
            },
            "thirdPartyFile": {
                "type": "File",
                "fileName": "c:/third_party_logs_simple_test.txt",
                "layout": "${logger}|${date}|${level:uppercase=true}|${message}",
                "autoFlush": true
            }
        },
        "rules": [
            {
                "logger": "ThirdPartyNamespace.*",
                "minLevel": "Info",
                "maxLevel": "Info",
                "writeTo": "thirdPartyFile"
            },
            {
                "logger": "*",
                "minLevel": "Info",
                "writeTo": "console"
            }
        ]
    },
    "AllowedHosts": "*"
}

It won't help you to change the log level but it will help you to manage the logs and reduce the entry being generated in the console

1 Comment

As one of the possible options. If Kibana is used, but I'll think about it.
0

NLogv 5 allows you to suppress a noisy logger using finalMinLevel:

{
  "NLog": {
    "throwConfigExceptions": true,
    "targets": {
      "console": {
        "type": "Console",
        "layout": "${logger}|${date}|${level:uppercase=true}|${message}"
      }
    },
    "rules": [
      {
        "logger": "ThirdPartyNamespace*",
        "finalMinLevel": "Warn"
      },
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "console"
      }
    ]
  }
}

This will suppress all LogEvents coming from Loggers matching wildcard ThirdPartyNamespace*, unless their LogLevel is Warning or more serious.

The same approach can be used to suppress the noise from Microsoft*-loggers. See also NLog v5 - LoggingProvider without any filter

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.