ColoredConsole Target
Writes logging messages to the console with customizable coloring.
| Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix | |||||
|---|---|---|---|---|---|---|---|---|---|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | ||
| NLog.dll | NLog.Win32.Targets.ColoredConsoleTarget | ![]() | ![]() | ![]() | ![]() | ![]() | ||||
Parameters (blue fields are required):
| Name | Type | Description | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| layout |
| |||||||||||||||||||||||||||
| name |
| |||||||||||||||||||||||||||
| errorStream |
| |||||||||||||||||||||||||||
| footer |
| |||||||||||||||||||||||||||
| header |
| |||||||||||||||||||||||||||
| useDefaultRowHighlightingRules |
| |||||||||||||||||||||||||||
| RowHighlightingRules | Collection of ConsoleRowHighlightingRule. Each element is represented as <highlight-row/> | |||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
| WordHighlightingRules | Collection of ConsoleWordHighlightingRule. Each element is represented as <highlight-word/> | |||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
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.



