Skip to content
Rolf Kristensen edited this page Jun 25, 2023 · 38 revisions

Writes log messages to the console.

Platforms Supported: Limited (Not available for NetStandard1.3)

There is also the colorful ColoredConsole-Target that can highlight single words or color entire rows.

Configuration Syntax

<targets>
  <target xsi:type="Console"
          name="String"
          layout="Layout"
          footer="Layout"
          header="Layout"
          encoding="Encoding"
          stderr="Boolean"
          detectConsoleAvailable="Boolean"
          writeBuffer="Boolean" />
</targets>

Read more about using the Configuration File.

Parameters

General Options

  • name - Name of the target.

Layout Options

  • layout - Text to be rendered. Layout Required. Default: ${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
  • footer - Footer. Layout
  • header - Header. Layout

Console Options

  • encoding - File encoding name like "utf-8", "ascii" or "utf-16". See Encoding class on MSDN. Defaults to Encoding.Default.

    Introduced in NLog 4.0.

  • 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 error.

  • 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 to false.

  • 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

  • WriteBuffer - Instead of calling Console.WriteLine then it writes buffer directly to Console.Write. When combined with AsyncWrapper it will allow batching and double output performance. Useful when redirecting console in cloud-function. Default: False.

    Introduced with NLog v4.6.8

Example

<target name="Console" xsi:type="Console"  layout="${longdate} ${logger} ${level} ${message}"/>
Clone this wiki locally