簡體   English   中英

NLog 結構化日志忽略按名稱的屬性

[英]NLog Structured Logging Ignore Property By Name

所以我正在使用 NLog 和 JSON 布局目標來記錄 my.Net 應用程序及其數據,這些數據到目前為止工作正常。

但是我有一個問題,我的一些對象包含大型 FileData 的字節 Arrays,這些文件包含在日志文件 output 中,出於文件大小的原因,我不想包括在內。

查看可用的 NLOG 教程,有一個“excludeProperties”屬性,我可以將其包含在布局配置中,但我似乎無法讓它真正發揮作用。

所以如果我的 Object 看起來像,

 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 output 仍然在生成的日志中包含 Name 屬性。 在此示例中,它只是名稱屬性,但是其他對象包括從數據庫中刪除的大文件數據,我想排除這些特定屬性...

那么我如何專門針對站 Object 的名稱屬性記住“名稱”屬性將存在於其他對象中。

我嘗試了以下屬性,它們仍然包含 output 中的 Name 屬性。

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

在 NLog 中使用結構化日志記錄時忽略屬性的正確“語法”是什么?

excludeProperties控制是否根據屬性名稱排除一個或多個LogEventInfo.Properties 它不控制從實際屬性值中排除哪些對象屬性。

這將捕獲Station -value 並將其與屬性名稱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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM