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

Sends log messages over the network.

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

Syntax

C#
public class NetworkTarget : TargetWithLayout
Visual Basic (Declaration)
Public Class NetworkTarget _
	Inherits TargetWithLayout
Visual Basic (Usage)
Dim instance As NetworkTarget

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="network" xsi:type="Network" address="tcp://localhost:5555" layout="${level} ${logger} ${message}${newline}" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Debug" writeTo="network" />
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        NetworkTarget target = new NetworkTarget();
 9        target.Layout = "${level} ${logger} ${message}${newline}";
10        target.Address = "tcp://localhost:5555";
11
12        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
13
14        Logger logger = LogManager.GetLogger("Example");
15        logger.Trace("log message 1");
16        logger.Debug("log message 2");
17        logger.Info("log message 3");
18        logger.Warn("log message 4");
19        logger.Error("log message 5");
20        logger.Fatal("log message 6");
21    }
22}

To print the results, use any application that's able to receive messages over TCP or UDP. NetCat is a simple but very powerful command-line tool that can be used for that. This image demonstrates the NetCat tool receiving log messages from Network target.

NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol or you'll get TCP timeouts and your application will be very slow. Either switch to UDP transport or use AsyncWrapper target so that your application threads will not be blocked by the timing-out connection attempts.

There are two specialized versions of the Network target: Chainsaw and NLogViewer which write to instances of Chainsaw log4j viewer or NLogViewer application respectively.

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.NetworkTarget
        NLog.Targets..::.NLogViewerTarget

See Also