如何防止 16 位数字被 nlog 记录?

How can I prevent 16 digit numbers from being logged by nlog?

我想从任何日志文件中过滤掉任何 16 个并发数字。例如,如果我的日志消息最初是:

"you blah 01234567890123456 foo"

我希望它只记录:

"you blah foo"

我不知道要实施什么,因为看起来过滤器会阻止记录整个消息。我想在记录之前更改要记录的消息。

您可以使用 replace layout render$message 包装在目标布局中,

Replaces a string in the output of another layout with another string.

使用以下配置:

<targets>
    <target xsi:type="Console" name="console" 
            layout="${replace:searchFor=\d\{17,17\}\s:replaceWith=:regex=true:inner=${message}}"/>
</targets>

和以下日志记录代码

var logger = LogManager.GetCurrentClassLogger();
logger.Info("you blah 01234567890123456 foo");

这是控制台输出的样子:

you blah foo

因为您可以使用任何可以与配置一起玩的正则表达式,以便正确删除或保留数字周围的空格,所以只需注意特殊字符的编码。