简体   繁体   English

NLog 替换布局渲染器不适用于 json 内容

[英]NLog Replace Layout Renderer not work with json content

I use a third party package that depending on Nlog 4.7 and NLog.AspNetCore 4.8, and the package will write log.我使用依赖于 Nlog 4.7 和 NLog.AspNetCore 4.8 的第三方 package,package 将写入日志。

In my Nlog.config在我的 Nlog.config 中

<logger name="package namespace" level="Info" writeTo="packageFile" />
<target xsi:type="File" name="packageFile" fileName="${baseFilePath}/packageFile.log" KeepFileOpen="true" layout="${message}" concurrentWrites="true" />

It's work, but now I need replace content in message.它的工作,但现在我需要替换消息中的内容。 I find Replace Layout Renderer that is I need.我找到了我需要的Replace Layout Renderer

The message is json string like this:消息是 json 字符串,如下所示:

{"ABC":{"DEF":"AA00014325","GHI":"01"}} 

I want to replace message become我想将消息替换为

{"ABC":{"DEF":"A****14325","GHI":"01"}} 

This is the pattern I have used: [AZ]\d{1}\d{2} I used this to test it against: https://regex101.com/这是我使用的模式:[AZ]\d{1}\d{2} 我用它来测试它: https://regex101.com/

Update: I also try the pattern by .NET Regex Tester更新:我也通过.NET Regex Tester尝试模式

My Nlog.config like this:我的 Nlog.config 是这样的:

<variable name ="messageReplace" value="${replace:inner=${message}:searchFor=[A-Z]\\d{1\}\\d{2\}:replaceWith=****:regex=true}" />
<logger name="package namespace" level="Info" writeTo="packageFile" />
<target xsi:type="File" name="packageFile" fileName="${baseFilePath}/packageFile.log" KeepFileOpen="true" layout="${messageReplace}" concurrentWrites="true" />  

But it's not work......但这行不通......

I change my Nlog.config like this:我像这样更改我的 Nlog.config:

<variable name ="message1" value="AA00014325" />
<variable name ="messageReplace" value="${replace:inner=${message1}:searchFor=[A-Z]\\d{1\}\\d{2\}:replaceWith=****:regex=true}" />
<logger name="package namespace" level="Info" writeTo="packageFile" />
<target xsi:type="File" name="packageFile" fileName="${baseFilePath}/packageFile.log" KeepFileOpen="true" layout="${messageReplace}" concurrentWrites="true" /> 

It's working...but if change message1 to它正在工作......但是如果将 message1 更改为

"AA00014325"

It not working again....Why?它不再工作......为什么?

I want to replace json string in message, how can I do?我想替换消息中的 json 字符串,我该怎么做?

Information:信息:

  • Platform: ASP.NET Core 3平台:ASP.NET 酷睿3
  • Nlog version: 4.7 Nlog版本:4.7
  • NLog.AspNetCore version: 4.8 NLog.AspNetCore 版本:4.8

Thank you!谢谢!

So using this configuration, I got it to work as per screenshot.所以使用这个配置,我让它按照屏幕截图工作。 Somethings to bear in mind is that the configuration is XML so to escape correctly .要记住的是配置是 XML 所以要正确转义 But I see you got that right ✔.但我看到你说对了✔。 So I believe its down to your regex.所以我相信这取决于您的正则表达式。

 <variable name="messageNoDigits" value="${replace:inner=${message}:searchFor=[A-Z]\\d\{3\}:replaceWith=****:regex=true}" />

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM