ColoredConsole Target


Writes logging messages to the console with customizable coloring.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Win32.Targets.ColoredConsoleTarget    

Parameters (blue fields are required):

NameTypeDescription
layoutstring  ${}
The text to be rendered.

Default value is: ${longdate}|${level:uppercase=true}|${logger}|${message}.

namestring
The name of the target.
errorStreamboolean
Determines whether the error stream (stderr) should be used instead of the output stream (stdout).

Default value is: False.

footerstring  ${}
Footer
headerstring  ${}
Header
useDefaultRowHighlightingRulesboolean
Use default row highlighting rules.

Default value is: True.

The default rules are:

ConditionForeground ColorBackground Color
level == LogLevel.FatalRedNoChange
level == LogLevel.ErrorYellowNoChange
level == LogLevel.WarnMagentaNoChange
level == LogLevel.InfoWhiteNoChange
level == LogLevel.DebugGrayNoChange
level == LogLevel.TraceDarkGrayNoChange

RowHighlightingRules Collection of ConsoleRowHighlightingRule. Each element is represented as <highlight-row/>
NameTypeDescription
conditionstring  [c()]
The condition that must be met in order to set the specified foreground and background color.
foregroundColorConsoleOutputColor
The foreground color.

Default value is: NoChange.

Possible values are:

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

backgroundColorConsoleOutputColor
The background color.

Default value is: NoChange.

Possible values are:

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

WordHighlightingRules Collection of ConsoleWordHighlightingRule. Each element is represented as <highlight-word/>
NameTypeDescription
regexstring
The regular expression to be matched. You must specify either text or regex.
textstring
The text to be matched. You must specify either text or regex.
wholeWordsboolean
Match whole words only.

Default value is: False.

ignoreCaseboolean
Ignore case when comparing texts.

Default value is: False.

foregroundColorConsoleOutputColor
The foreground color.

Default value is: NoChange.

Possible values are:

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

backgroundColorConsoleOutputColor
The background color.

Default value is: NoChange.

Possible values are:

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

Example:

To set up the target in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="console" xsi:type="ColoredConsole" 
            layout="${date:format=HH\:MM\:ss} ${logger} ${message}"> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Trace" writeTo="console"/> 
  </rules> 
</nlog> 

This assumes just one target and a single rule. More configuration options are described here.

To set up the log target programmatically use code like this:

using NLog; 
using NLog.Win32.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        ColoredConsoleTarget target = new ColoredConsoleTarget(); 
        target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Trace("trace log message"); 
        logger.Debug("debug log message"); 
        logger.Info("info log message"); 
        logger.Warn("warn log message"); 
        logger.Error("error log message"); 
        logger.Fatal("fatal log message"); 
    } 
}

The result is a colorful console, where each color represents a single log level.

In addition you can configure your own word highlighting rules so that particular words or regular expressions will be marked with a distinguished color:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="console" xsi:type="ColoredConsole" 
            layout="${date:format=HH\:MM\:ss} ${logger} ${message}"> 
      <highlight-word text="log" backgroundColor="DarkGreen"/> 
      <highlight-word text="abc" foregroundColor="Cyan"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Trace" writeTo="console"/> 
  </rules> 
</nlog> 

Programmatic equivalent of the above configuration:

using NLog; 
using NLog.Win32.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        ColoredConsoleTarget target = new ColoredConsoleTarget(); 
        target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
        target.WordHighlightingRules.Add( 
                new ConsoleWordHighlightingRule("log",  
                    ConsoleOutputColor.NoChange,  
                    ConsoleOutputColor.DarkGreen)); 
        target.WordHighlightingRules.Add( 
                new ConsoleWordHighlightingRule("abc",  
                    ConsoleOutputColor.Cyan,  
                    ConsoleOutputColor.NoChange)); 
                 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Trace("trace log message abcdefghijklmnopq"); 
        logger.Debug("debug log message"); 
        logger.Info("info log message abc abcdefghijklmnopq"); 
        logger.Warn("warn log message"); 
        logger.Error("error log abcdefghijklmnopq message abc"); 
        logger.Fatal("fatal log message abcdefghijklmnopq abc"); 
    } 
}

Here's the result:

Custom row highlighting lets you colorize the output by any condition. This example shows how to mark all entries containing the word "serious" with white color on red background and mark all entries coming from a particular logger with yellow on blue.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="console" xsi:type="ColoredConsole" 
            layout="${date:format=HH\:MM\:ss} ${logger} ${message}"> 
      <highlight-row 
                     condition="level >= LogLevel.Error and contains(message,'serious')" 
                     foregroundColor="White" backgroundColor="Red"/> 
      <highlight-row condition="starts-with(logger,'Example')" 
                     foregroundColor="Yellow" backgroundColor="DarkBlue"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Trace" writeTo="console"/> 
  </rules> 
</nlog> 

Programmatic equivalent of the above configuration:

using NLog; 
using NLog.Win32.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        ColoredConsoleTarget target = new ColoredConsoleTarget(); 
        target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
        target.RowHighlightingRules.Add( 
                new ConsoleRowHighlightingRule( 
                    "level >= LogLevel.Error and contains(message,'serious')", // condition 
                    ConsoleOutputColor.White, // foreground color 
                    ConsoleOutputColor.Red // background color 
                    ) 
                ); 
 
        target.RowHighlightingRules.Add( 
                new ConsoleRowHighlightingRule( 
                    "starts-with(logger,'Example')", // condition 
                    ConsoleOutputColor.Yellow, // foreground color 
                    ConsoleOutputColor.DarkBlue) // background color 
                ); 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
        // LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("ColoredConsoleTargetRowHighlighting.nlog"); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Trace("trace log message"); 
        logger.Debug("debug log message"); 
        logger.Info("info log message"); 
        logger.Warn("warn log message"); 
        logger.Error("very serious error log message"); 
        logger.Fatal("fatal log message, rather serious"); 
 
        Logger logger2 = LogManager.GetLogger("Another"); 
        logger2.Trace("trace log message"); 
        logger2.Debug("debug log message"); 
        logger2.Info("info log message"); 
        logger2.Warn("warn log message"); 
        logger2.Error("very serious error log message"); 
        logger2.Fatal("fatal log message"); 
    } 
}

Here's the result:


Back to the target list.