![](/img/trans.png)
[英]How to conditionally render an attribute with NLog and structured logging?
[英]How do I suppress certain fields in NLOG structured logging?
<?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"} } }
应该在日志中的唯一 email 是 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"} } }
我不明白为什么这应该得到嵌套表达式
我可以建议您阅读有关如何使用结构化日志记录的维基页面
应该如何配置以隐藏此目标的 EmailId 和 UserId 属性?
logger.Info("Problems Processing {event}", obj);
表示使用 property-key event
和 property-value obj
创建 LogEvent。 excludeProperties
只能用于属性键,不能用于深入研究属性值和挑选特定的类属性。
您可以像这样手动将obj
值切片为属性键:
logger.Info("Problems Processing {EmailId} {OtherValue}", obj.EmailId, obj.OtherValue);
或者,如果您有一个“已知”对象类型,那么您可以将对象类型的转换注册为仅具有所需属性的安全对象:
LogManager.Setup().SetupSerialization(s =>
s.RegisterObjectTransformation<MyUnsafeObject>(obj => new {
OtherValue = obj.OtherValue
})
);
另一种选择是覆盖内置的 NLog-Json-Converter并提供一个可以根据需要呈现对象的方法。
另外,“事件”的嵌套是怎么回事
您已指定<attribute name="event" encode="false">
,因此它将创建一个event
-json-attribute,它将呈现所有NLog LogEvent 属性( includeAllProperties="true"
)。 和消息模板logger.Info("Problems Processing {event}", obj);
将捕获值为obj
的属性键event
(因此event
属性具有嵌套的event
属性)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.