Skip to content
Rolf Kristensen edited this page Oct 12, 2025 · 11 revisions

Sends log messages to the remote instance of Log4J XML Viewer applications (Like Chainsaw).

Platforms Supported: All - Requires nuget-package NLog.Targets.Network

Introduced with NLog 6.0 where Chainsaw-Target and NLogViewer-Target was merged together into this.

This target inherits from the Network Target, and so it has also all the properties of the Network Target available. Using the Log4JXmlEventLayout to produce Log4J XML output.

Configuration Syntax

<targets>
  <target xsi:type="Log4JXml"
          name="String"
          address="Layout"
          newLine="Boolean"
          onOverflow="Enum"
          maxMessageSize="Integer"
          encoding="Encoding"
          connectionCacheSize="Integer"
          lineEnding="CRLF|LF|CR|None"
          maxConnections="Integer"
          maxQueueSize="Integer"
          keepConnection="Boolean"
          onConnectionOverflow="Enum"
          includeSourceInfo="Boolean"
          includeCallSite="Boolean"
          appInfo="String"
          ndcItemSeparator="String"
          includeNdc="Boolean">
    <parameter name="String" layout="Layout" /><!-- repeated -->
  </target>
</targets>

Read more about using the Configuration File.

Parameters

General Options

  • name - Name of the target.

Layout Options

  • layout - Instance of Log4JXmlEventLayout that is used to format log messages. Layout Default: Log4JXmlEventLayout

  • encoding - Encoding to be used. Encoding Default: utf-8

  • newLine - Indicates whether to append newline at the end of log message. Boolean Default: False

  • lineEnding - Line Ending to be used if newLine is set to true. LineEndingMode Default: CRLF. Not used if newLine is false. Possible values:

    • CRLF - Carriage Return and Line Feed (ASCII 13, ASCII 10). (default)
    • CR - Carriage Return (ASCII 13).
    • LF - Line Feed (ASCII 10).
    • NULL - Null terminator (ASCII 0)
    • None - No end of line characters.
  • maxMessageSize - Maximum message size in bytes. Integer Default: 65000

  • onOverflow - Action that should be taken if the message-size is larger than maxMessageSize. Possible values:

    • Discard - Discard the entire message.
    • Error - Report an error.
    • Split - Split the message into smaller pieces (Default)

Connection Options

  • connectionCacheSize - Size of the connection cache (number of connections which are kept alive). Integer Default: 5

  • address - Network address. Layout The network address can be:

    • tcp://host:port - TCP (auto select IPv4/IPv6)
    • tcp4://host:port - force TCP/IPv4
    • tcp6://host:port - force TCP/IPv6
    • udp://host:port - UDP (auto select IPv4/IPv6)
    • udp4://host:port - force UDP/IPv4
    • udp6://host:port - force UDP/IPv6
    • http://host:port/pageName - HTTP using POST verb
    • https://host:port/pageName - HTTPS using POST verb For SOAP-based webservice support over HTTP use WebService target.
  • keepConnection - Indicates whether to keep connection open whenever possible. Boolean Default: True

  • maxConnections - Maximum current connections. 0 = no maximum. Integer Default: 16. Not used if keepConnection is true.

  • onConnectionOverflow - Action that should be taken when open connections are higher than limit maxConnections. Possible enum values:

    • Discard - Discard new messages when reaching limit (Default)
    • Grow - Send message and ignore limit
    • Block - Block until pending connections has beeen freed.
  • maxQueueSize - Maximum queue size for a single connection. 0 means no limit. Integer. Default: 10000

  • onQueueOverflow - Action that should be taken if pending queue of messages is larger than maxQueueSize.

    • Discard - Discard the entire message (Default)
    • Grow - Ignore limit and grow the queue.
    • Block - Block until queue is below limit.
  • KeepAliveTimeSeconds - Idle time before first TCP keep-alive probe is sent. Faster detection of stale TCP connections. Currently only implemented for TCP. Integer. Default: 0 (Disabled)

  • SendTimeoutSeconds - Waiting time before TCP socket send-operation fails with timeout error. Default wait forever when network cable unplugged and tcp-buffer becomes full. Integer. Default: 0 (Disabled)

  • NoDelay - Disable the delayed ACK timer, and avoid delay of 200 ms. Default = true.

  • sslProtocols - Enables SSL/TLS protocols. Default no SSL/TLS is used. Currently only implemented for TCP. Invalid certificates are not supported. Possible values (combination allowed, comma separated):

    • None - No SSL/TLS is used
    • Default - Accepts Tls or Ssl3
    • Ssl2 - really not recommended
    • Ssl3 - not recommended
    • Tls - TLS 1.0
    • Tls11 - TLS 1.1
    • Tls12 - TLS 1.2
  • SslCertificateFile - Enables loading SSL certificate from local file, instead of injecting into operating system cache (Blank value means disable SSL certificate validation)

  • SslCertificatePassword - When password is needed for loading SSL certificate from local file using SslCertificateFile

Payload Options

  • IncludeEventProperties - Include all events properties of a logevent? Default: true.

  • IncludeScopeProperties - Indicates whether to include ScopeContext Properties dictionary. Default: false.

  • includeSourceInfo - Indicates whether to include source info (file name and line number) in the information sent over the network. Boolean

  • includeCallSite - Indicates whether to include call site (class and method name) in the information sent over the network. Boolean

  • IncludeScopeNested - Indicates whether to include ScopeContext Nested Stack as NDC. Default: false.

  • ScopeNestedSeparator - NDC item separator. Default:

  • includeNdc - Indicates whether to include ScopeContext Nested Stack. Default: false.

  • ndcItemSeparator - NDC item separator. Default:

  • appInfo - AppInfo field. By default it's the friendly name of the current AppDomain.

  • loggerName - Logger field. By default it's the output of ${logger}

  • formattedMessage - Message field. By default it's the output of ${message}

  • parameter - Additional context properties that should be included.

    • name - Name of Parameter. Required.
    • layout - The layout for the Parameter-Value. Required.

Example

<?xml version="1.0" encoding="utf-8"?>
<nlog throwConfigExceptions="true">

  <extensions>
    <add assembly="NLog.Targets.Network"/>
  </extensions>

  <targets>
    <target name="log4j" xsi:type="Log4JXml" address="udp://127.0.0.1:5001" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="log4j" />
  </rules>
</nlog>

Available Log4J-XML viewers (See also LogViewer Tools)

Notice that most Log4J-XML viewers will drop messages and stall when NLog sends messages at full speed. One can consider to throttle NLog like this:

<?xml version="1.0" encoding="utf-8"?>
<nlog throwConfigExceptions="true">

  <extensions>
    <add assembly="NLog.Targets.Network"/>
  </extensions>

  <targets>
    <target name="throttle_log4j" type="AsyncWrapper" timeToSleepBetweenBatches="100" batchSize="1" overflowAction="Block">
      <target name="log4j" type="Log4JXml" address="tcp://127.0.0.1:5001" newline="true" />
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="throttle_log4j" />
  </rules>
</nlog>

Clone this wiki locally