【问题标题】:How do I suppress certain fields in NLOG structured logging?如何抑制 NLOG 结构化日志记录中的某些字段?
【发布时间】:2021-08-04 19:35:52
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!--This NLog config is for testing purposes only. Changes to this config will affect test cases and not the real application.-->
    <!--Changes to the config in production ARE NOT reflected here-->
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          internalLogLevel="Warn"
          internalLogFile="/tmp/nlog-internal.log">
        <variable name="baseLayout" value="${longdate} ${level:upperCase=true} ${message}" />
        <!--async attribute set to "false" for log testing purposes. Changing this will break log tests 4/4/2021-->
        <targets async="false">
            <target name="NoPiiLog" xsi:type="Memory">
                <layout type="JsonLayout">
                    <attribute name="time" layout="${longdate}" />
                    <attribute name="level" layout="${level}" />
                    <!-- <attribute name="message" layout="${message}" /> -->
                    <attribute name="event" encode="false" >
                        <layout type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"
                         excludeProperties="UserId,EmailId" excludeEmptyProperties="true"/>
                    </attribute>
                </layout>
            </target>
            <target name="WithPiiLog" xsi:type="Memory" layout="${structuredlogging.json}">
                <layout type="JsonLayout">
                    <attribute name="time" layout="${longdate}" />
                    <attribute name="level" layout="${level}" />
                    <attribute name="message" layout="${message}" />
                    <attribute name="event" encode="false" >
                        <layout type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"/>
                    </attribute>
                </layout>
            </target>
        </targets>
        <rules>
            <logger name="*" minlevel="Info" writeTo="NoPiiLog,WithPiiLog" />
        </rules>
    </nlog>
</configuration>

还有日志记录代码:

            var obj = new
            {
                EmailId = "mark@gmail.com",
                OtherValue = "You should see this"
            };

    var logger = LogManager.GetLogger("Test");

    logger.Info("Problems Processing @{event}", obj);

    logger.Info(ex, "Houston, we have a problem @{event}", new {UserId="mark@gmail.com", SomethingElse= "oh no! Mr Bill!"});

    logger.Error("@{event}", new{Message="Poorly Named property", SlipsThru="mark@gmail.com"});

还有日志消息:

{ "time": "2021-08-04 13:37:59.2163", "level": "Info", "event": { "event": {"EmailId":"mark@gmail.com", "OtherValue":"You should see this"} } }
{ "time": "2021-08-04 13:37:59.2585", "level": "Info", "event": { "event": {"UserId":"mark@gmail.com", "SomethingElse":"oh no! Mr Bill!"} } }
{ "time": "2021-08-04 13:37:59.2600", "level": "Error", "event": { "event": {"Message":"Poorly Named property", "SlipsThru":"mark@gmail.com"} } }

应该在日志中的唯一电子邮件是 SlipsThru 属性上的电子邮件,但它们都出现在日志中,我不明白为什么。应该如何配置它以隐藏此 target 的 EmailId 和 UserId 属性?

我也尝试将includeAllProperties="true" maxRecursionLimit="2" excludeProperties="UserId,EmailId" excludeEmptyProperties="true" 添加到顶级JsonLayout,再次没有效果.....

另外,“事件”的嵌套是怎么回事

"event": { "event": {"EmailId":"mark@gmail.com", "OtherValue":"You should see this"} } }

我看不出这应该得到嵌套表达式的任何理由

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    我可以推荐你阅读这个关于 How to use structured logging 的维基页面

    应如何配置以隐藏此目标的 EmailId 和 UserId 属性?

    logger.Info("Problems Processing {event}", obj); 表示使用属性键 event 和属性值 obj 创建 LogEvent。 excludeProperties 只能用于属性键,不能用于深入研究属性值和挑选特定的类属性。

    您可以像这样手动将obj-value 分割成属性键:

    logger.Info("Problems Processing {EmailId} {OtherValue}", obj.EmailId, obj.OtherValue);
    

    如果您有“已知”对象类型,则可以将对象类型的register transformation 转换为仅具有所需属性的安全对象:

    LogManager.Setup().SetupSerialization(s =>
       s.RegisterObjectTransformation<MyUnsafeObject>(obj => new {
          OtherValue = obj.OtherValue
       })
    );
    

    另一种选择是 override the builtin NLog-Json-Converter 并提供一个可以根据需要渲染对象的方法。

    另外,“事件”的嵌套是怎么回事

    您已指定&lt;attribute name="event" encode="false"&gt;,因此它将创建一个event-json-attribute,它将呈现所有NLog LogEvent Properties (includeAllProperties="true")。并且消息模板logger.Info("Problems Processing {event}", obj); 将捕获属性键event,其值为obj(因此event-attribute 具有嵌套的event-property)

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多