Skip to content
Rolf Kristensen edited this page May 18, 2019 · 34 revisions

Loading from appsettings.json

Introduced with NLog.Extensions.Logging ver. 1.5.0

var config = new ConfigurationBuilder()
   .SetBasePath(System.IO.Directory.GetCurrentDirectory())
   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
   .Build();

LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

Json NLog Config Example

{
    "Logging": {
        "NLog": {
            "IncludeScopes": true,
        }
    },
    "NLog": {
        "autoReload": true,
        "throwConfigExceptions": true,
        "internalLogLevel": "info",
        "internalLogFile": "c:/temp/internal-nlog.txt",
        "extensions": {
            "NLog.Extensions.Logging": {
                "assembly": "NLog.Extensions.Logging"
            }
        },
        "variables": {
            "var_logdir": "c:/temp"
        },
        "default-wrapper": {
            "type": "AsyncWrapper",
            "overflowAction": "Block"
        },
        "targets": {
            "all-file": {
                "type": "File",
                "fileName": "${var_logdir}/nlog-all-${shortdate}.log",
                "layout": "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
            },
            "own-console": {
                "type": "Console",
                "layout": "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|${callsite}"
            }
        },
        "rules": [
            {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "all-file"
            },
            {
                "logger": "Microsoft.*",
                "minLevel": "Info",
                "final": true
            },
            {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "own-console"
            }
        ]
    }
}

Clone this wiki locally