ColoredConsole target

Rolf Kristensen edited this page Jan 14, 2018 · 17 revisions
Clone this wiki locally

Writes log messages to the console with customizable coloring.

Supported in .NET and Mono

Configuration Syntax

<targets>
  <target xsi:type="ColoredConsole"
          name="String"
          encoding="Encoding"
          layout="Layout"
          header="Layout"
          footer="Layout"
          useDefaultRowHighlightingRules="Boolean"
          errorStream="Boolean"
          detectConsoleAvailable="Boolean">
    <highlight-row backgroundColor="Enum" condition="Condition" foregroundColor="Enum"/><!-- repeated -->
    <highlight-word backgroundColor="Enum" foregroundColor="Enum" ignoreCase="Boolean"
                    regex="String" text="String" wholeWords="Boolean" compileRegex="Boolean"/><!-- repeated -->
  </target>
</targets>

Read more about using the Configuration File.

Parameters

General Options

  • name - Name of the target.

  • encoding - File encoding name like "utf-8", "ascii" or "utf-16". See Encoding class on MSDN. Defaults to Encoding.Default (UTF-8 on silverlight). Starting for NLog 4.0.

Layout Options

  • layout - Text to be rendered. Layout Required. Default: ${longdate}|${level:uppercase=true}|${logger}|${message}
  • header - Header. Layout
  • footer - Footer. Layout

Performance Options

  • OptimizeBufferReuse - Reduce logging overhead, by allowing buffer reuse. Default: True

    Introduced with NLog v4.4.2. Default became True with NLog v4.5

Highlighting Rules

  • useDefaultRowHighlightingRules - Indicates whether to use default row highlighting rules. [Boolean](Data types) Default: True The default rules are:
Condition Foreground Color Background Color
level == LogLevel.Fatal Red NoChange
level == LogLevel.Error Yellow NoChange
level == LogLevel.Warn Magenta NoChange
level == LogLevel.Info White NoChange
level == LogLevel.Debug Gray NoChange
level == LogLevel.Trace DarkGray NoChange
  • rowHighlightingRules - The row highlighting rules. [Collection](Data types)
    Each collection item is represented by <highlight-row /> element with the following attributes:

    • backgroundColor - Background color. Default: NoChange
      Possible values:

      • Black - Black Color (#000000).
      • Blue - Blue Color (#0000FF).
      • Cyan - Cyan Color (#00FFFF).
      • DarkBlue - Dark blue Color (#000080).
      • DarkCyan - Dark Cyan Color (#008080).
      • DarkGray - Dark Gray Color (#808080).
      • DarkGreen - Dark green Color (#008000).
      • DarkMagenta - Dark Magenta Color (#800080).
      • DarkRed - Dark Red Color (#800000).
      • DarkYellow - Dark Yellow Color (#808000).
      • Gray - Gray Color (#C0C0C0).
      • Green - Green Color (#00FF00).
      • Magenta - Magenta Color (#FF00FF).
      • NoChange - Don't change the color.
      • Red - Red Color (#FF0000).
      • White - White Color (#FFFFFF).
      • Yellow - Yellow Color (#FFFF00).
    • condition - Condition that must be met in order to set the specified foreground and background color. Condition Required.

    • foregroundColor - Foreground color. Default: NoChange
      Possible values:

      • Black - Black Color (#000000).
      • Blue - Blue Color (#0000FF).
      • Cyan - Cyan Color (#00FFFF).
      • DarkBlue - Dark blue Color (#000080).
      • DarkCyan - Dark Cyan Color (#008080).
      • DarkGray - Dark Gray Color (#808080).
      • DarkGreen - Dark green Color (#008000).
      • DarkMagenta - Dark Magenta Color (#800080).
      • DarkRed - Dark Red Color (#800000).
      • DarkYellow - Dark Yellow Color (#808000).
      • Gray - Gray Color (#C0C0C0).
      • Green - Green Color (#00FF00).
      • Magenta - Magenta Color (#FF00FF).
      • NoChange - Don't change the color.
      • Red - Red Color (#FF0000).
      • White - White Color (#FFFFFF).
      • Yellow - Yellow Color (#FFFF00).
  • wordHighlightingRules - The word highlighting rules. [Collection](Data types)
    Each collection item is represented by <highlight-word /> element with the following attributes:

    • backgroundColor - Background color. Default: NoChange
      Possible values:

      • Black - Black Color (#000000).
      • Blue - Blue Color (#0000FF).
      • Cyan - Cyan Color (#00FFFF).
      • DarkBlue - Dark blue Color (#000080).
      • DarkCyan - Dark Cyan Color (#008080).
      • DarkGray - Dark Gray Color (#808080).
      • DarkGreen - Dark green Color (#008000).
      • DarkMagenta - Dark Magenta Color (#800080).
      • DarkRed - Dark Red Color (#800000).
      • DarkYellow - Dark Yellow Color (#808000).
      • Gray - Gray Color (#C0C0C0).
      • Green - Green Color (#00FF00).
      • Magenta - Magenta Color (#FF00FF).
      • NoChange - Don't change the color.
      • Red - Red Color (#FF0000).
      • White - White Color (#FFFFFF).
      • Yellow - Yellow Color (#FFFF00).
    • foregroundColor - Foreground color. Default: NoChange
      Possible values:

      • Black - Black Color (#000000).
      • Blue - Blue Color (#0000FF).
      • Cyan - Cyan Color (#00FFFF).
      • DarkBlue - Dark blue Color (#000080).
      • DarkCyan - Dark Cyan Color (#008080).
      • DarkGray - Dark Gray Color (#808080).
      • DarkGreen - Dark green Color (#008000).
      • DarkMagenta - Dark Magenta Color (#800080).
      • DarkRed - Dark Red Color (#800000).
      • DarkYellow - Dark Yellow Color (#808000).
      • Gray - Gray Color (#C0C0C0).
      • Green - Green Color (#00FF00).
      • Magenta - Magenta Color (#FF00FF).
      • NoChange - Don't change the color.
      • Red - Red Color (#FF0000).
      • White - White Color (#FFFFFF).
      • Yellow - Yellow Color (#FFFF00).
    • ignoreCase - Indicates whether to ignore case when comparing texts. [Boolean](Data types) Default: false

    • regex - Regular expression to be matched. You must specify either text or regex.

    • text - Text to be matched. You must specify either text or regex.

    • wholeWords - Indicates whether to match whole words only. [Boolean](Data types) Default: false

    • compileRegex - Introduced in NLog 4.3. Compiles the Regex. If false, the regex cache is used. Setting this to true can improve performance, but costs memory. Default: false

Output Options

  • errorStream - Indicates whether the error stream (stderr) should be used instead of the output stream (stdout). [Boolean](Data types) Default: False

  • detectConsoleAvailable - Indicates whether the console target should disable itself when no console detected. Boolean Default: false (introduced in 4.3.10 with default: true. Since NLog 4.4 default false)

Programmatic example

var consoleTarget = new ColoredConsoleTarget();

var highlightRule = new ConsoleRowHighlightingRule();
highlightRule.Condition = ConditionParser.ParseExpression("level == LogLevel.Info");
highlightRule.ForegroundColor = ConsoleOutputColor.Green;
consoleTarget.RowHighlightingRules.Add(highlightRule);