如何防止 ${aspnet-request-posted-body} 不记录敏感信息
How to prevent ${aspnet-request-posted-body} to not log sensitive info
我正在使用 ${aspnet-request-post-body}
在日志文件中记录请求正文。我面临的问题:我想阻止 ${aspnet-request-post-body}
记录一些信息,即密码和信用卡详细信息,我想对它们应用屏蔽。
例如,如果请求正文是
{username : ABC, password :554&3}
这应该以这种格式记录
{username: ABC, password : ****}
请注意,我已经针对这个问题尝试过替换布局,但不想使用它。还有其他方法可以完成此任务吗?
${aspnet-request-post-body}
只是 NLog 的文本。
所以你有以下选择:
替换所有内容(使用正则表达式):配置示例替换全部:
<variable name="messageNoDigits"
value="${replace:inner=${message}:searchFor=(\d{3\})+:replaceWith=:regex=true}" />
编写您自己的自定义布局渲染器,将正文解析为 JSON 并对其进行转换。请注意,阅读 ASP.NET Core 中的正文可能有点棘手,请参阅 the current code in NLog for it。如果您有 reading/transforming 正文的代码,它可以很容易地转换为 NLog 布局渲染器:
using NLog.Web.LayoutRenderers;
// register ${SanitizedBody}
AspNetLayoutRendererBase.Register("SanitizedBody", (logEventInfo, httpContext, loggingConfiguration) => MyMethod(httpContext));
见https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
我正在使用 ${aspnet-request-post-body}
在日志文件中记录请求正文。我面临的问题:我想阻止 ${aspnet-request-post-body}
记录一些信息,即密码和信用卡详细信息,我想对它们应用屏蔽。
例如,如果请求正文是
{username : ABC, password :554&3}
这应该以这种格式记录
{username: ABC, password : ****}
请注意,我已经针对这个问题尝试过替换布局,但不想使用它。还有其他方法可以完成此任务吗?
${aspnet-request-post-body}
只是 NLog 的文本。
所以你有以下选择:
替换所有内容(使用正则表达式):配置示例替换全部:
<variable name="messageNoDigits" value="${replace:inner=${message}:searchFor=(\d{3\})+:replaceWith=:regex=true}" />
编写您自己的自定义布局渲染器,将正文解析为 JSON 并对其进行转换。请注意,阅读 ASP.NET Core 中的正文可能有点棘手,请参阅 the current code in NLog for it。如果您有 reading/transforming 正文的代码,它可以很容易地转换为 NLog 布局渲染器:
using NLog.Web.LayoutRenderers; // register ${SanitizedBody} AspNetLayoutRendererBase.Register("SanitizedBody", (logEventInfo, httpContext, loggingConfiguration) => MyMethod(httpContext));
见https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer