RegEx Replace Layout Renderer - NLog/NLog GitHub Wiki
RegEx for replacing a string in the output of another layout with another string.
Platforms Supported: All - Requires nuget-package NLog.RegEx
There is also ${replace} if not needing RegEx-support.
${regex-replace:searchFor=String:wholeWords=Boolean:replaceWith=String
:ignoreCase=Boolean:inner=Layout}
-
searchFor - Text to search for.
String - replaceWith - Replacement string.
-
ignoreCase - Indicates whether to ignore case when searching.
BooleanDefault:False -
wholeWords - Indicates whether to search for whole words.
BooleanDefault:False -
replaceGroupName - Specifies group name for Matched Subexpressions, where it will perform replace of all matches with that group name. Ex.
(?<groupname>subexpression) -
compileRegex - Compiles the Regex. Setting this to
truecan improve regex performance, but costs memory.BooleanDefault:False
Replace sensitive data, that starts with password= or password:
<variable name="replacePasswords"
value="${regex-replace:inner=${message}:searchFor=(?i)(?<=password[=\:])(.*?)(?=(\;|$| )):replaceWith=******}" />Truncate after token, search for , and replace everything after with empty string:
<variable name="truncateAfterComma"
value="${regex-replace:inner=${aspnet-request-ip}:searchFor=,.*:replaceWith=}" />With Regular Expressions keep in mind the content escaping rules to escape special characters in Regex. Specifically } and \. So for example the regular expression (\d{3})+ would need to be escaped like so in your configuration
<variable name="messageNoDigits"
value="${regex-replace:inner=${message}:searchFor=(\\d{3\})+:replaceWith=}" />There is a dedicated layout ${replace-newlines} for replacing / removing newlines (handles both Unix and Windows newlines)