我使用NLog以json格式登录,使用下面的目标(文件和网络)。
当登录到文件目标或同时登录到文件和网络时,可以正常记录消息。
但是,当只使用网络目标时,dbResponseTime和messagesSentTotal将不会包含在消息中(实际上,任何使用“event-properties:item=”布局的属性)。所有其他属性都存在,包括在end (module)中定义的属性。
<?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}'发布于 2019-11-02 21:20:04
修复此问题的NLog 4.5.4已发布:https://github.com/NLog/NLog/issues/2573
https://stackoverflow.com/questions/48586433
复制相似问题