skip to Main Content

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

Answers


  1. 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>
    
    Login or Signup to reply.
  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. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search