You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NLog网络目标时JSON日志event-properties字段缺失问题

解决NLog网络目标丢失event-properties属性的问题

嘿,我之前踩过类似的NLog配置坑,来帮你捋一捋!

先再明确下你的问题现象:

  • 单独用文件目标,或者文件+网络目标一起用:JSON日志里dbResponseTime、messagesSentTotal这些通过event-properties:item=定义的自定义属性都正常显示
  • 仅用网络目标:这些自定义属性直接消失了,但像module这类其他属性完全没问题

最可能的原因:网络目标的JsonLayout配置遗漏了event properties的引入

NLog的JsonLayout不会自动包含event properties,除非你明确配置IncludeEventProperties="true",或者显式添加每个属性的节点。大概率你的文件目标配置了这个关键项,但网络目标没加。

具体解决方案

方案1:自动包含所有event properties(推荐)

在网络目标的JsonLayout标签里加上IncludeEventProperties="true",这样所有通过event-properties设置的自定义属性都会自动塞进JSON里,不用一个个手动添加:

<target name="logHealthCheck" xsi:type="Network">
  <!-- 核心配置:开启自动包含event properties -->
  <layout xsi:type="JsonLayout" IncludeEventProperties="true">
    <attribute name="module" layout="${mdlc:item=module}" />
    <!-- 你的其他固定属性配置 -->
  </layout>
</target>

方案2:显式添加需要的属性

如果你只想包含特定的event properties,不想全部自动引入,可以逐个添加对应的attribute节点:

<target name="logHealthCheck" xsi:type="Network">
  <layout xsi:type="JsonLayout">
    <attribute name="module" layout="${mdlc:item=module}" />
    <!-- 手动添加需要的自定义event properties -->
    <attribute name="dbResponseTime" layout="${event-properties:item=dbResponseTime}" />
    <attribute name="messagesSentTotal" layout="${event-properties:item=messagesSentTotal}" />
    <!-- 其他需要的属性 -->
  </layout>
</target>

额外排查技巧

如果上面的方案没解决问题,建议开启NLog的内部日志定位细节:

  1. 修改NLog根节点的internalLogLevel="Debug"(你当前设的是Off)
  2. 可以再加个internalLogFile="nlog-internal.log"指定日志输出文件
  3. 运行程序后查看内部日志,看看网络目标在输出时有没有报错,比如找不到某个event property,或者布局解析异常

内容的提问来源于stack exchange,提问作者maus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:18:35