NLog 结构化日志记录按名称忽略 属性

NLog Structured Logging Ignore Property By Name

所以我正在使用 NLog 和 JSON 布局目标来记录我的 .Net 应用程序及其数据,到目前为止大部分工作正常。

但是我有一个问题,我的一些对象包含大型 FileData 的字节数组,这些字节数组被包含在日志文件输出中,出于文件大小的原因我不想包含。

查看可用的 NLOG 教程,有一个“excludeProperties”属性,我可以将其包含在布局配置中,但我似乎无法让它真正发挥作用。

所以如果我的对象看起来像,

 public class Station : IStation
        {
       
            public int ID { get; set; }
            public string Name { get; set; }
            public string MACAddress { get; set; }
            public string ComputerName { get; set; }
    }

并且我在 NLog 中使用了以下布局配置,

 <layout type='JsonLayout'>
                <attribute name='Time' layout='${longdate}' />
                <attribute name='Level' layout='${level:upperCase=true}'/>
                <attribute name='Call Site' layout='${callsite}'/>

                <attribute name='Data' encode='false' >
                    <layout type='JsonLayout'
                            includeAllProperties="true"
                            maxRecursionLimit="20" 
                            excludeProperties="Name"
                        >
                        <attribute name='message' layout='${message}' encode='false' />
                        <attribute name='exception' layout='${exception}' />
                    </layout>
                </attribute>
            </layout>

然后使用结构化日志记录调用:

 _logger.Debug("Station Details:{@0}", Station);

  

NLog 输出在生成的日志中仍然包含 Name 属性。在此示例中,它只是名称 属性,但其他对象包括从数据库中提取的大文件数据,我想排除这些特定属性...

那么我如何专门针对站对象的名称 属性 记住“名称”属性 将存在于其他对象中。

我已经尝试了以下属性,它们都在输出中包含名称 属性。

 excludeProperties="_Name"
    excludeProperties="Name"
    excludeProperties="Station_Name"
    excludeProperties="IStation_Name"

在 NLog 中使用结构化日志记录时忽略属性的正确“语法”是什么?

excludeProperties-选项控制是否根据属性-名称排除一个或多个LogEventInfo.Properties。它不控制从实际 属性 值中排除哪些对象属性。

这将捕获 Station-value 并使用 属性-name secret1:

存储它
 _logger.Debug("Station Details:{@secret1}", Station);

这将排除任何具有 属性 名称 secret1secret2:

的 属性 值
 excludeProperties="secret1,secret2"

如果您想控制从 属性 值类型中包含哪些对象属性,那么您可以改为这样做:

    NLog.LogManager.Setup().SetupSerialization(s =>
       s.RegisterObjectTransformation<Station>(station => new {
          ID = station.ID,
          MACAddress = station.MACAddress,
          ComputerName = station.ComputerName,
       })
    );

另请参阅:https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#transform-captured-properties