【问题标题】:Nlog JsonLayout does not include LogEvent PropertiesNlog JsonLayout 不包含 LogEvent 属性
【发布时间】:2022-05-20 00:50:41
【问题描述】:

我尝试实现此示例https://github.com/nlog/nlog/wiki/JsonLayout#nested-json-with-structured-logging,但在我的情况下,输出在 eventProperties 字段中没有数据。 NLog 配置:

    <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="TRACE" internalLogFile="c:\temp\nlog-internal.log">


  <variable name="LogLevel" value="DEBUG"/>
  <variable name="brief" value="${longdate} | ${level} | ${logger} | ${message}"/>
  <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
  <variable name="logLifetime"  value="3"/>



  <targets>
    <target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt"  archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd"
        maxArchiveDays="${logLifetime}"
       archiveEvery="Day" >
    <layout xsi:type="JsonLayout">
      <attribute name="time" layout="${longdate}" />
      <attribute name="level" layout="${level:upperCase=true}"/>
      <attribute name="message" layout="${message}" />
      <attribute name="eventProperties" encode="false" >
        <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
      </attribute>
    </layout>
    </target>
    <target name="Color" xsi:type="Console">
      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeEventProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>
    </target>
  </targets>
  
  

  <rules>
    <logger name="Global" minlevel="${LogLevel}"  writeTo="Global,Color" />
    
  </rules>
</nlog>

c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    [Serializable]
    public class TestObject
    {
        public string A { get; set; }
        public int B { get; set; }

        public override string ToString() { return A; }
    }

    class Program
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetLogger("Global");
        static void Main(string[] args)
        {
            var testObj = new TestObject
            {
                A = "AlphaObject",
                B = 2
            };

            var testObjB = new TestObject
            {
                A = "BetaObject",
                B = 3
            };

            Logger.Info("First: {alpha}, Second: {beta}", testObj, testObjB);
         
            Console.ReadLine();
        }
    }
}

我的 json 输出:{ "time": "2022-02-09 14:21:27.7096", "level": "INFO", "message": "First: AlphaObject, Second: BetaObject", "eventProperties": { } }

示例的 JSON 输出:

{
  "time": "2018-04-02 02:00:00.2349",
  "level": "Info",
  "message": "First: AlphaObject, Second: BetaObject",
  "eventProperties": {
    "alpha": {
      "A": "AlphaObject",
      "B": 2
    },
    "beta": {
      "A": "BetaObject",
      "B": 3
    }
  }
}

我使用 nlog 的最后一个 nuget 包,这个应用程序使用 .NET 框架 4.5.2。 nlog 模式还突出显示了布局参数之一

并且 nlog-internal 有下一个警告:

Warn Error has been raised. Exception: NLog.NLogConfigurationException: Error when setting value 'true' for property 'includeEventProperties' on element 'NLog.Config.NLogXmlElement' ---> System.NotSupportedException: Parameter includeEventProperties not supported on JsonLayout
   в NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory configurationItemFactory)
   в NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object targetObject, ILoggingConfigurationElement element, Boolean ignoreType)
   --- Конец трассировки внутреннего стека исключений ---

【问题讨论】:

  • 是的,非常抱歉 NLog Wiki 中的混乱。已经开始准备 NLog 5.0 将选项名称从 IncludeAllProperties 更改为 IncludeEventProperties。如果不使用 NLog v5.0(例如 v4.7),那么应该使用IncludeAllProperties(两个选项名称都适用于 NLog 5.0)。更改名称的原因是因为 NLog 5.0 还引入了新选项 IncludeScopeProperties(然后 IncludeAllProperties 变得混乱)。 NLog 5.0 RTM 应该会在几周内发布。

标签: c# json .net nlog


【解决方案1】:

IncludeEventProperties 用于 NLog 5.0,当使用旧版本的 NLog 时,应使用IncludeAllProperties(旧于 NLog v4.7.14)

      <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="eventProperties" encode="false" >
          <layout xsi:type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"/>
        </attribute>
      </layout>

更改名称的原因是因为 NLog 5.0 还引入了新选项 IncludeScopeProperties(然后 IncludeAllProperties 变得混乱)。

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2019-11-22
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2021-11-18
    • 2020-08-28
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多