使用网络目标时,NLog json字段丢失

时间:2018-02-02 15:59:35

标签: nlog

我使用NLog登录json格式,使用下面的目标(文件和网络)。

当记录到文件目标或文件和网络时,消息记录正确。

但是,当仅使用网络目标时,dbResponseTime和messagesSentTotal将不会包含在消息中(事实上,任何使用" event-properties:item ="的布局的属性)。存在所有其他属性,包括最后定义的属性(模块)。

  <?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="Off">

  <targets>
    <target name="logHealthCheck" xsi:type="File"
     fileName="D:\Logs\HealthCheck.${shortdate}.json"
     archiveFileName="D:\Logs\HealthCheck.{#}.json"
     archiveNumbering="DateAndSequence"
     archiveAboveSize="33554432"
     archiveDateFormat="yyyy-MM-dd">
      <layout xsi:type="JsonLayout">
        <attribute name="timestamp" layout="${date:universalTime=true:format=o}" />
        <attribute name="machineName" layout="${machinename}" />
        <attribute name="environment" layout="${literal:text=DEV}" />
        <attribute name="dbResponseTime" layout="${event-properties:item=dbResponseTime}" encode="false"/>
        <attribute name="messagesSentTotal" layout="${event-properties:item=messagesSentTotal}" encode="false"/>
        <attribute name="module" layout="${literal:text=core}" />
      </layout>
    </target>

    <target name="logHealthCheckNetwork" xsi:type="Network" address="tcp://127.0.0.1:1514" newLine="true">
      <layout xsi:type="JsonLayout">
        <attribute name="timestamp" layout="${date:universalTime=true:format=o}" />
        <attribute name="machineName" layout="${machinename}" />
        <attribute name="environment" layout="${literal:text=DEV}" />
        <attribute name="dbResponseTime" layout="${event-properties:item=dbResponseTime}" encode="false"/>
        <attribute name="messagesSentTotal" layout="${event-properties:item=messagesSentTotal}" encode="false"/>
        <attribute name="module" layout="${literal:text=core}" />
      </layout>
    </target>

   <rules>
    <logger name="HealthCheck" minlevel="Debug" writeTo="logHealthCheck,logHealthCheckNetwork">
      <filters>
      </filters>
    </logger>
  </rules>

</nlog>

在内部日志上切换,不记录任何错误。跟踪如下:

2018-02-02 16:43:07.7837 Trace Scanning LoggingRule 'logNamePattern: (HealthCheck:Equals) levels: [ Debug Info Warn Error Fatal ] appendTo: [ logHealthCheckNetwork ]'
2018-02-02 16:43:07.7993 Trace  Scanning NetworkTarget 'Network Target[logHealthCheckNetwork]'
2018-02-02 16:43:07.8305 Trace   Scanning SimpleLayout ''tcp://127.0.0.1:1514''
2018-02-02 16:43:07.8305 Trace    Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-02-02 16:43:07.9085 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:07.9085 Trace     Scanning SimpleLayout ''${date:universalTime=true:format=o}''
2018-02-02 16:43:07.9241 Trace      Scanning DateLayoutRenderer 'Layout Renderer: ${date}'
2018-02-02 16:43:07.9397 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:07.9397 Trace     Scanning SimpleLayout ''${machinename}''
2018-02-02 16:43:07.9553 Trace      Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-02-02 16:43:07.9553 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:07.9709 Trace     Scanning SimpleLayout ''${literal:text=DEV}''
2018-02-02 16:43:07.9709 Trace      Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-02-02 16:43:08.0177 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:08.0333 Trace     Scanning SimpleLayout ''${event-properties:item=dbResponseTime}''
2018-02-02 16:43:08.0333 Trace      Scanning EventPropertiesLayoutRenderer 'Layout Renderer: ${event-properties}'
2018-02-02 16:43:08.0801 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:08.0801 Trace     Scanning SimpleLayout ''${event-properties:item=messagesSentTotal}''
2018-02-02 16:43:08.3453 Trace    Scanning JsonAttribute 'NLog.Layouts.JsonAttribute'
2018-02-02 16:43:08.3609 Trace     Scanning SimpleLayout ''${literal:text=core}''
2018-02-02 16:43:08.3765 Trace      Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'

1 个答案:

答案 0 :(得分:0)

NLog 4.5.4已发布,解决了以下问题:https://github.com/NLog/NLog/issues/2573