Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert json array struct to object struct, Convenient to load configuration files according to the environment #293

Closed
czd890 opened this issue May 17, 2019 · 5 comments
Labels

Comments

@czd890
Copy link

czd890 commented May 17, 2019

If NLog:rules section is array, Loading the configuration file according to the environment needs to rewrite all the NLog:rules section, Maybe use object replace array struct.

Json config file rules, Please See Load JSON based NLog config

nlog.json

{
    "rules": {
        "one": {},
        "error": {
            "writeTo": "errorFile,errorEmail"
        }
    }
}

nlog.dev.json

{
    "rules": {
        "error": {
            "writeTo": "errorFile"
        }
    }
}
@snakefoot
Copy link
Contributor

snakefoot commented May 17, 2019

Really like the idea of merging logging-rules. But the NLog rules needs to be evaluated in correct order, and MEL always performs sorting when dictionary {}, that is why array [] is currently used:

Could one solve it by introducing something like rule-groups? (Here group-name "default" + "override")

nlog.json

    "rules": {
        "default": [
            {
                "name": "rule1",
                "logger": "*",
                "minLevel": "Debug",
                "writeTo": "File"
            }
        ],

nlog.dev.json

    "rules": {
        "override": [
            {
                "name": "rule1",
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "File,Console"
            }
        ],
    }

By using "name" one can even override individual logging-rules, but one can also add additional logging-rules in nlog.dev.json.

Not that experienced with appsettings.json. So any input is great. I just implemented json-support as a proof-of-concept.

@snakefoot
Copy link
Contributor

snakefoot commented May 17, 2019

Peaked over the fence to our neighbor, and saw others doing this:

        "rules": {
            "0": {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "all-file"
            },
            "1": {
                "logger": "Microsoft.*",
                "minLevel": "Info",
                "final": true
            },
            "2": {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "own-console"
            }
        }

So you can already do this with the current implementation:

nlog.json

        "rules": {
            "0": {
                "logger": "*",
                "minLevel": "Error",
                "writeTo": "errorFile,errorEmail"
            },
            "1": {
                "logger": "*",
                "minLevel": "Debug",
                "writeTo": "debugFile"
            }
        }

nlog.dev.json (Overwrites rule-"0" and adds rule-"200");

        "rules": {
            "0": {
                "logger": "*",
                "minLevel": "Error",
                "writeTo": "errorFile"
            },
            "200": {
                "logger": "*",
                "minLevel": "Debug",
                "writeTo": "debugConsole"
            }
        }

@snakefoot
Copy link
Contributor

snakefoot commented May 17, 2019

Btw. it is useful to use integer-values to ensure correct ordering of logging-rules. But one can also add extra details:

        "rules": {
            "00_all-file": {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "all-file"
            },
            "01_blackhole": {
                "logger": "Microsoft.*",
                "minLevel": "Info",
                "final": true
            },
            "02_own-console": {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "own-console"
            }
        }

@czd890
Copy link
Author

czd890 commented May 20, 2019

Thank you very much, it solved my problem.

@snakefoot
Copy link
Contributor

snakefoot commented May 20, 2019

@czd890 Have updated the wiki with the extra details:

https://github.com/NLog/NLog.Extensions.Logging/wiki/Json-NLog-Config

Please feel free to edit. If I have forgotten important details, or you have some nice tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants