NLog:找不到 LayoutRenderer:'aspnet-user-identity

NLog: LayoutRenderer cannot be found: 'aspnet-user-identity

我尝试将 NLog 实现到我的 .NET Core Api Web 服务中。 我想登录到 Oracle 数据库。通过 nlog.config XML 文件一切正常。

但目标是将 NLog 配置实施到 appsettings.json 中,这里出现了问题。 我收到标题中设置的错误:

LayoutRenderer cannot be found: 'aspnet-user-identity

我的配置文件是这样的:

"NLog": {
    "autoReload": true,
    "throwConfigExceptions": true,
    "internalLogLevel": "info",
    "internalLogFile": "c:/app/log/dev/internal-appsetting-nlog.txt",
    "extensions": {
        "NLog.Extensions.Logging": {
            "assembly": [
                "NLog.Extensions.Logging",
                "NLog.Web.AspNetCore"
            ]
        }
    },
    "variables": {
        "var_logdir": "c:/app/log/dev"
    },
    "default-wrapper": {
        "type": "AsyncWrapper",
        "overflowAction": "Block"
    },
    "targets": {
        "all-file": {
            "type": "File",
            "fileName": "${var_logdir}/nlog-all-${shortdate}.log",
            "layout": {
                "type": "JsonLayout",
                "Attributes": [
                    {
                        "name": "timestamp",
                        "layout": "${date:format=o}"
                    },
                    {
                        "name": "level",
                        "layout": "${level}"
                    },
                    {
                        "name": "logger",
                        "layout": "${logger}"
                    },
                    {
                        "name": "message",
                        "layout": "${message:raw=true}"
                    },
                    {
                        "name": "properties",
                        "encode": false,
                        "layout": {
                            "type": "JsonLayout",
                            "includeallproperties": "true"
                        }
                    }
                ]
            }
        },
        "db": {
            "type": "Database",
            "commandText": "INSERT INTO logtable (LOGLEVEL,LOGGER,MESSAGE,MACHINENAME,USERNAME,CALLSITE, THREADID,EXCEPTIONMESSAGE,STACKTRACE,SESSIONID) VALUES (:pLEVEL,:pLOGGER,:pMESSAGE,:pMACHINENAME, :pCALLSITE,:pTHREADID,:pEXCEPTIONMESSAGE,:pSTACKTRACE)",
            "parameters": [
                {
                    "name": "@pLEVEL",
                    "layout": "${level}"
                },
                {
                    "name": "@pLOGGER",
                    "layout": "${logger}"
                },

                {
                    "name": "@pMESSAGE",
                    "layout": "${message}"
                },
                {
                    "name": "@pMACHINENAME",
                    "layout": "${machinename}"
                },
                {
                    "name": "@pUSERNAME",
                    "layout": "${aspnet-user-identity}"
                },
                {
                    "name": "@pCALLSITE",
                    "layout": "${callsite:filename=true}"
                },
                {
                    "name": "@pTHREADID",
                    "layout": "${threadid}"
                },
                {
                    "name": "@pEXCEPTIONMESSAGE",
                    "layout": "${exception}"
                },
                {
                    "name": "@pSTACKTRACE",
                    "layout": "${stacktrace}"
                },
                {
                    "name": "@pSESSIONID",
                    "layout": "${aspnet-sessionid}"
                }
            ],
            "dbProvider": "Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess",
            "connectionString": "xxxxxxxxxxxx"
        }
    },
    "rules": [
        {
            "logger": "*",
            "minLevel": "Trace",
            "writeTo": "all-file"
        },
        {
            "logger": "*",
            "minLevel": "Trace",
            "writeTo": "db"
        },
        {
            "logger": "Microsoft.*",
            "maxLevel": "Info",
            "final": true
        }
    ]
},

内部调试器报告:

2019-10-09 16:48:48.6665 Info Adding target AsyncTargetWrapper(Name=all-file)
2019-10-09 16:48:48.7859 Warn Error when setting property 'Layout' on 'NLog.Targets.DatabaseParameterInfo' Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-identity'. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name)
   at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader stringReader)
   at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr, Boolean isNested, String& text)
   at NLog.Layouts.SimpleLayout.set_Text(String value)
   at NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type propertyType, String value, Object& newValue, ConfigurationItemFactory configurationItemFactory)
   at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)

${aspnet-sessionid} 发生错误。如果我注释掉这两个布局,一切正常。

我在 GitHub 问题报告中发现了不同的东西,但我尝试的一切都失败了。

有人可以帮忙吗?

未知的 aspnet-user-identity 可能是您的扩展程序的问题:

"extensions": [
  { "assembly": "NLog.Extensions.Logging" },
  { "assembly": "NLog.Web.AspNetCore" }
],

你能试试上面的建议吗?

P.S。更新了 wiki 以包含多个 "extensions"

的示例