Writes log messages to the console with customizable coloring.
Platforms Supported: All
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" wholeWords="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
${longdate}|${level:uppercase=true}|${logger}|${message}
StdErr - Indicates whether the error stream (stderr) should be used instead of the output stream (stdout). Layout Default: False
Before NLog v5.0 the property was named
errorStream
. NLog 6.0 changed property from boolean to NLog Layout
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
noColor - Incicates whether console target should disable coloring when detecting environment-variable NO_COLOR=1
Introduced with NLog v5.4
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
NLog v6 changed the default rules not use yellow
for Error
and maganta
for Warn
Colors :
rowHighlightingRules - The row highlighting rules. Collection
Each collection item is represented by <highlight-row /> element with the following attributes:
wordHighlightingRules - The word highlighting rules. Collection
Each collection item is represented by <highlight-word /> element with the following attributes:
false
false
NLog v6 no longer supports word-highlight using Regular Expressions (RegEx) and removed these options:
false
, the regex cache is used. Setting this to true
can improve performance, but costs memory. Default: false
var consoleTarget = new ColoredConsoleTarget(); var highlightRule = new ConsoleRowHighlightingRule(); highlightRule.Condition = ConditionParser.ParseExpression("level == LogLevel.Info"); highlightRule.ForegroundColor = ConsoleOutputColor.Green; consoleTarget.RowHighlightingRules.Add(highlightRule);Microsoft Extension Logging AddConsole
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
Visual Studio Output windowThe 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}"
)
See Stackoverflow
Or use The Visual Studio extension VSColorOutput (VS2015-2019). Then you could also use the regular Console target.
Missing colors for dotnet watch runWhen 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.
See also: https://github.com/dotnet/aspnetcore/issues/25317
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4