【发布时间】: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"} } }
我看不出这应该得到嵌套表达式的任何理由
【问题讨论】: