Click or drag to resize

ColoredConsoleTarget Class

Writes log messages to the console with customizable coloring.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsTargetWithLayoutHeaderAndFooter
        NLog.TargetsColoredConsoleTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public sealed class ColoredConsoleTarget : TargetWithLayoutHeaderAndFooter

The ColoredConsoleTarget type exposes the following members.

Constructors
  NameDescription
Public methodColoredConsoleTarget
Initializes a new instance of the ColoredConsoleTarget class.
Public methodColoredConsoleTarget(String)
Initializes a new instance of the ColoredConsoleTarget class.
Top
Properties
  NameDescription
Public propertyAutoFlush
Gets or sets a value indicating whether to auto-flush after WriteLine
Public propertyDetectConsoleAvailable
Gets or sets a value indicating whether to auto-check if the console is available. - Disables console writing if Environment.UserInteractive = False (Windows Service) - Disables console writing if Console Standard Input is not available (Non-Console-App)
Public propertyDetectOutputRedirected
Gets or sets a value indicating whether to auto-check if the console has been redirected to file - Disables coloring logic when System.Console.IsOutputRedirected = true
Public propertyEnableAnsiOutput
Enables output using ANSI Color Codes
Public propertyEncoding
The encoding for writing messages to the Console.
Public propertyFooter
Gets or sets the footer.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyHeader
Gets or sets the header.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyLayout
Gets or sets the text to be rendered.
(Inherited from TargetWithLayoutHeaderAndFooter.)
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyRowHighlightingRules
Gets the row highlighting rules.
Public propertyStdErr
Gets or sets a value indicating whether to send the log messages to the standard error instead of the standard output.
Public propertyUseDefaultRowHighlightingRules
Gets or sets a value indicating whether to use default row highlighting rules.
Public propertyWordHighlightingRules
Gets the word highlighting rules.
Top
Methods
  NameDescription
Public methodDispose
Closes the target.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Public methodPrecalculateVolatileLayouts
Calls the Precalculate(LogEventInfo) on each volatile layout used by this target. This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Top
Remarks
Examples

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

XML
1!ERROR: See log file!

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

C#
 1using NLog;
 2using NLog.Win32.Targets;
 3
 4class Example
 5{
 6    static void Main(string[] args)
 7    {
 8        ColoredConsoleTarget target = new ColoredConsoleTarget();
 9        target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
10
11        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
12
13        Logger logger = LogManager.GetLogger("Example");
14        logger.Trace("trace log message");
15        logger.Debug("debug log message");
16        logger.Info("info log message");
17        logger.Warn("warn log message");
18        logger.Error("error log message");
19        logger.Fatal("fatal log message");
20    }
21}
See Also