Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Replace Layout Renderer

Rolf Kristensen edited this page Jun 22, 2025 · 27 revisions

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

Platforms Supported: All

Configuration Syntax

${replace:searchFor=String:wholeWords=Boolean:replaceWith=String
         :ignoreCase=Boolean:inner=Layout}

Parameters

Search/Replace Options

  • searchFor - Text to search for. String
  • replaceWith - Replacement string.
  • ignoreCase - Indicates whether to ignore case when searching. Boolean Default: False
  • wholeWords - Indicates whether to search for whole words. Boolean Default: False

Transformation Options

  • inner - Wrapped layout. Layout

RegEx not supported

NLog v6 does not support Regular Expressions (RegEx), and have removed these options:

  • regex - Indicates whether regular expressions should be used when searching. Boolean Default: False
  • replaceGroupName - Specifies group name for Matched Subexpressions, where it will perform replace of all matches with that group name. Ex. (?<groupname>subexpression)

    Introduced with NLog 3.1

  • compileRegex - Compiles the Regex. Setting this to true can improve regex performance, but costs memory. Boolean Default: False

    Introduced with NLog 4.7

If needing Regular Expressions, then one can use ${regex-replace} from NLog.RegEx-nuget-package.

RegEx Examples

Replace sensitive data, that starts with password= or password:

<variable name="replacePasswords"
  value="${replace:inner=${message}:searchFor=(?i)(?&lt;=password[=\:])(.*?)(?=(\;|$| )):replaceWith=******:regex=true}" />

Truncate after token, search for , and replace everything after with empty string:

<variable name="truncateAfterComma"
  value="${replace:inner=${aspnet-request-ip}:searchFor=,.*:replaceWith=:regex=true}" />

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="${replace:inner=${message}:searchFor=(\\d{3\})+:replaceWith=:regex=true}" />

There is a dedicated layout ${replace-newlines} for replacing / removing newlines (handles both Unix and Windows newlines)

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.