Last updated: 2006-09-18

This website is based on NLog v1.0. Click here to view the documentation for other versions.

Network Target


Sends logging messages over the network.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Targets.NetworkTarget

Parameters (blue fields are required):

NameTypeDescription
layoutstring  ${}
The text to be rendered.

Default value is: ${longdate}|${level:uppercase=true}|${logger}|${message}.

namestring
The name of the target.
addressstring
The network address. Can be tcp://host:port or udp://host:port

For HTTP Support use the WebService target.

encodingstring
Encoding

Default value is: utf-8.

Can be any encoding name supported by System.Text.Encoding.GetEncoding() e.g. windows-1252, iso-8859-2.

keepConnectionboolean
Keep connection open whenever possible.

Default value is: True.

maxMessageSizeinteger
Maximum message size in bytes.

Default value is: 65000.

newLineboolean
Append newline at the end of log message.

Default value is: False.

onOverflowOverflowAction
Action that should be taken if the message is larger than maxMessageSize

Possible values are:

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

Example:

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

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="network" xsi:type="Network" address="tcp://localhost:5555" 
            layout="${level} ${logger} ${message}${newline}"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="network"/> 
  </rules> 
</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:

using NLog; 
using NLog.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        NetworkTarget target = new NetworkTarget(); 
        target.Layout = "${level} ${logger} ${message}${newline}"; 
        target.Address = "tcp://localhost:5555"; 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Trace("log message 1"); 
        logger.Debug("log message 2"); 
        logger.Info("log message 3"); 
        logger.Warn("log message 4"); 
        logger.Error("log message 5"); 
        logger.Fatal("log message 6"); 
    } 
}

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 crawl. 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.


Back to the target list.
Webwww.nlog-project.org