[This is preliminary documentation and is subject to change.]

Writes log messages to the console.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public sealed class ConsoleTarget : TargetWithLayoutHeaderAndFooter
Visual Basic (Declaration)
Public NotInheritable Class ConsoleTarget _
	Inherits TargetWithLayoutHeaderAndFooter
Visual Basic (Usage)
Dim instance As ConsoleTarget

Examples

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

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4    <targets>
 5        <target name="console" xsi:type="Console" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Info" writeTo="console" />
10    </rules>
11</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:

CopyC#
 1using NLog;
 2using NLog.Targets;
 3
 4class Example
 5{
 6    static void Main(string[] args)
 7    {
 8        ConsoleTarget target = new ConsoleTarget();
 9        target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
10
11        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
12
13        Logger logger = LogManager.GetLogger("Example");
14        logger.Debug("log message");
15    }
16}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.TargetWithLayoutHeaderAndFooter
        NLog.Targets..::.ConsoleTarget

See Also