-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ColoredConsole target
Writes log messages to the console with customizable coloring.
Platforms Supported: Limited (Not available for NetStandard1.3)
There is also the simple Console-Target without any overhead from coloring.
<targets>
<target xsi:type="ColoredConsole"
name="String"
encoding="Encoding"
layout="Layout"
header="Layout"
footer="Layout"
useDefaultRowHighlightingRules="Boolean"
stderr="Boolean"
enableAnsiOutput="Boolean"
detectConsoleAvailable="Boolean"
detectOutputRedirected="Boolean">
<highlight-row condition="Condition" backgroundColor="Enum" foregroundColor="Enum"/><!-- repeated -->
<highlight-word text="String" condition="Condition" backgroundColor="Enum" foregroundColor="Enum"
ignoreCase="Boolean" regex="String" wholeWords="Boolean" compileRegex="Boolean"/><!-- repeated -->
</target>
</targets>
Read more about using the Configuration File.
-
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
.Introduced with NLog 4.0
-
layout - Text to be rendered. Layout Required. Default:
${longdate}|${level:uppercase=true}|${logger}|${message}
- header - Header. Layout
- footer - Footer. Layout
-
StdErr - Indicates whether the error stream (stderr) should be used instead of the output stream (stdout). Boolean Default: False
Before NLog v5.0 the property was named
errorStream
. -
detectConsoleAvailable - Indicates whether the console target should disable itself when no console detected. Boolean Default:
false
Introduced in NLog 4.3.10 with default:
true
, then NLog 4.4 changed default tofalse
. -
detectOutputRedirected - Indicates whether the console target should disable coloring when it detects that
Console.IsOutputRedirected = true
(Skip coloring when pipe to file)Introduced in NLog 4.6.7
-
enableAnsiOutput - Enables output using ANSI Color Codes. Needed for e.g. Visual Studio Code. Boolean Default:
false
Introduced in NLog 4.6
-
autoFlush - Performs explicit flush after every console write. Useful if having redirected to custom console-stream that doesn't has autoflush enabled. Boolean. Default = False.
Introduced in NLog 4.6.3
- useDefaultRowHighlightingRules - Indicates whether to use default row highlighting rules. Boolean Default: True The default rules are using these Colors :
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
Each collection item is represented by <highlight-row /> element with the following attributes:- backgroundColor - Background color. Color Enum Default: NoChange
- foregroundColor - Foreground color. Color Enum Default: NoChange
- condition - Condition that must be met in order to set the specified foreground and background color. Condition Required.
-
wordHighlightingRules - The word highlighting rules. Collection
Each collection item is represented by <highlight-word /> element with the following attributes:- backgroundColor - Background color. Color Enum Default: NoChange
- foregroundColor - Foreground color. Color Enum Default: NoChange
- text - Text to be matched. You must specify either text or regex.
- regex - Regular expression to be matched. You must specify either text or regex.
-
ignoreCase - Indicates whether to ignore case when comparing texts. Boolean Default:
false
-
wholeWords - Indicates whether to match whole words only. Boolean Default:
false
-
compileRegex - Introduced in NLog 4.3. Compiles the Regex. If
false
, the regex cache is used. Setting this totrue
can improve performance, but costs memory. Default:false
- condition - Introduced in NLog 4.7.1. Condition that must be met before scanning for words to highlight. Condition.
Color Enum | Description | HEX Code | ANSI Foreground | ANSI Background |
---|---|---|---|---|
NoChange | Don't change the color | |||
Black | Black Color | #000000 | \x1B[30m | \x1B[40m |
Blue | Blue Color | #0000FF | \x1B[94m | \x1B[104m |
Cyan | Cyan Color | #00FFFF | \x1B[96m | \x1B[106m |
DarkBlue | Dark Blue Color | #000080 | \x1B[34m | \x1B[44m |
DarkCyan | Dark Cyan Color | #008080 | \x1B[36m | \x1B[46m |
DarkGray | Dark Gray Color | #808080 | \x1B[90m | \x1B[100m |
DarkGreen | Dark Green Color | #008000 | \x1B[32m | \x1B[42m |
DarkMagenta | Dark Magenta Color | #800080 | \x1B[35m | \x1B[45m |
DarkRed | Dark Red Color | #800000 | \x1B[31m | \x1B[41m |
DarkYellow | Dark Yellow Color | #808000 | \x1B[33m | \x1B[43m |
Gray | Gray Color | #C0C0C0 | \x1B[37m | \x1B[47m |
Green | Green Color | #00FF00 | \x1B[92m | \x1B[102m |
Magenta | Magenta Color | #FF00FF | \x1B[95m | \x1B[105m |
Red | Red Color | #FF0000 | \x1B[91m | \x1B[101m |
White | White Color | #FFFFFF | \x1b[97m | \x1B[107m |
Yellow | Yellow Color | #FFFF00 | \x1B[93m | \x1B[103m |
var consoleTarget = new ColoredConsoleTarget();
var highlightRule = new ConsoleRowHighlightingRule();
highlightRule.Condition = ConditionParser.ParseExpression("level == LogLevel.Info");
highlightRule.ForegroundColor = ConsoleOutputColor.Green;
consoleTarget.RowHighlightingRules.Add(highlightRule);
The NLog Layout of the ColoredConsole-Target can be configured to match the output of the default MEL AddConsole().
See also NLog Console and AddConsole
The Visual Studio "Output Window" is bit tricky with colors. It is not a real console window - it just displays the console output from the application.
You could get colors by prefixing the message in your config (Ex. Layout="${level:format=FulleName}: ${message}"
)
- "Error: " - for errors - red
- "Warning: " - for warnings - orange
See Stackoverflow
Or use The Visual Studio extension VSColorOutput (VS2015-2019). Then you could also use the regular Console target.
When running application using dotnet watch run
then one might experience that console colors are missing.
This can be fixed by changing launchSettings.json
and removing the entry launchBrowser
and the colors should return.
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json