Mateusz Przybylek
Mateusz Przybylek

Reputation: 5905

How can I override all array properties in appsettings.json at once

I know that using appsettings.Development.json files we can overide default settings. Also I know that we can change single properties in json array using array:i incidator.

The question is, is it possible to override all json values at once. Like in example below for loggin rules?

appsettings.Development.json

...
  "NLog": {
...
    "rules": [
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "logconsole"
      },
      {
        "logger": "*",
        "minLevel": "Error",
        "writeTo": "allfile"
      }
    ]
...

Example solution appsettings.Development.json

{
  "NLog": {
    "rules:*": {
      "minLevel": "Error"
    }
  }
}

Upvotes: 2

Views: 650

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19877

With NLog ver. 4.6.7 you don't have to override all array-properties. You can use NLog Config variables as minLevel in the rules-section. Ex:

{
  "NLog": {
    "variables": {
      "MinLevelInfo": "Info"
      "MinLevelError": "Error"
    },
    "rules": [
      {
        "logger": "*",
        "minLevel": "${MinLevelInfo}",
        "writeTo": "logconsole"
      },
      {
        "logger": "*",
        "minLevel": "${MinLevelError}",
        "writeTo": "allfile"
      }
    ]
  }
}

Example override in appsettings.Development.json

{
  "NLog": {
    "variables": {
      "MinLevelInfo": "Debug"
      "MinLevelError": "Debug"
    }
}

Now you just have to override NLog variables, and they will be automatically applied by NLog. See also https://github.com/NLog/NLog/pull/2709 and https://github.com/NLog/NLog/pull/3184

See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json

See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration

Upvotes: 1

Related Questions