Click or drag to resize

NetworkTarget Class

Sends log messages over the network.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsNetworkTarget
        NLog.TargetsNLogViewerTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.1+cf6675da40ccfd4c8c526a3b2bdbeed3442910a1
Syntax
public class NetworkTarget : TargetWithLayout

The NetworkTarget type exposes the following members.

Constructors
  NameDescription
Public methodNetworkTarget
Initializes a new instance of the NetworkTarget class.
Public methodNetworkTarget(String)
Initializes a new instance of the NetworkTarget class.
Top
Properties
  NameDescription
Public propertyAddress
Gets or sets the network address.
Public propertyCompress
Type of compression for protocol payload. Useful for UDP where datagram max-size is 8192 bytes.
Public propertyCompressMinBytes
Skip compression when protocol payload is below limit to reduce overhead in cpu-usage and additional headers
Public propertyConnectionCacheSize
Gets or sets the size of the connection cache (number of connections which are kept alive). Requires KeepConnection = true
Public propertyEncoding
Gets or sets the encoding to be used.
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Public propertyKeepAliveTimeSeconds
The number of seconds a connection will remain idle before the first keep-alive probe is sent
Public propertyKeepConnection
Gets or sets a value indicating whether to keep connection open whenever possible.
Public propertyLayout
Gets or sets the layout used to format log messages.
(Inherited from TargetWithLayout.)
Public propertyLineEnding
Gets or sets the end of line value if a newline is appended at the end of log message NewLine.
Protected propertyLoggingConfiguration
Gets the logging configuration this target is part of.
(Inherited from Target.)
Public propertyMaxConnections
Gets or sets the maximum simultaneous connections. Requires KeepConnection = false
Public propertyMaxMessageSize
Gets or sets the maximum message size in bytes. On limit breach then OnOverflow action is activated.
Public propertyMaxQueueSize
Gets or sets the maximum queue size for a single connection. Requires KeepConnection = true
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyNewLine
Gets or sets a value indicating whether to append newline at the end of log message.
Public propertyOnConnectionOverflow
Gets or sets the action that should be taken, when more connections than MaxConnections.
Public propertyOnOverflow
Gets or sets the action that should be taken if the message is larger than MaxMessageSize
Public propertyOnQueueOverflow
Gets or sets the action that should be taken, when more pending messages than MaxQueueSize.
Public propertySslProtocols
Gets or sets the SSL/TLS protocols. Default no SSL/TLS is used. Currently only implemented for TCP.
Protected propertySyncRoot
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.)
Top
Methods
  NameDescription
Protected methodCloseTarget
Closes the target to release any initialized resources
(Overrides TargetCloseTarget.)
Public methodDispose
Closes the target.
(Inherited from Target.)
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Protected methodFlushAsync
Flush any pending log messages asynchronously (in case of asynchronous targets).
(Overrides TargetFlushAsync(AsyncContinuation).)
Protected methodGetBytesToWrite
Gets the bytes to be written.
Protected methodInitializeTarget
Initializes the target before writing starts
(Inherited from Target.)
Public methodPrecalculateVolatileLayouts
Calls the Precalculate(LogEventInfo) on each volatile layout used by this target. This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.)
Protected methodRenderLogEvent(Layout, LogEventInfo)
Renders the logevent into a string-result using the provided layout
(Inherited from Target.)
Protected methodRenderLogEventT(LayoutT, LogEventInfo, T)
Renders the logevent into a result-value by using the provided layout
(Inherited from Target.)
Protected methodResolveServiceT
Resolve from DI ServiceRepository
(Inherited from Target.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Protected methodWrite(AsyncLogEventInfo)
Sends the rendered logging event over the network optionally concatenating it with a newline character.
(Overrides TargetWrite(AsyncLogEventInfo).)
Protected methodWrite(IListAsyncLogEventInfo)
Writes an array of logging events to the log target. By default it iterates on all events and passes them to "Write" method. Inheriting classes can use this method to optimize batch writes.
(Inherited from Target.)
Protected methodWrite(LogEventInfo)
Writes logging event to the target destination
(Inherited from Target.)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Protected methodWriteAsyncThreadSafe(AsyncLogEventInfo)
Writes a log event to the log target, in a thread safe manner. Any override of this method has to provide their own synchronization mechanism. !WARNING! Custom targets should only override this method if able to provide their own synchronization mechanism. Layout-objects are not guaranteed to be thread-safe, so using them without a SyncRoot-object can be dangerous.
(Inherited from Target.)
Protected methodWriteAsyncThreadSafe(IListAsyncLogEventInfo)
Writes an array of logging events to the log target, in a thread safe manner. Any override of this method has to provide their own synchronization mechanism. !WARNING! Custom targets should only override this method if able to provide their own synchronization mechanism. Layout-objects are not guaranteed to be thread-safe, so using them without a SyncRoot-object can be dangerous.
(Inherited from Target.)
Protected methodWriteFailedNotInitialized
LogEvent is written to target, but target failed to successfully initialize
(Inherited from Target.)
Top
Events
  NameDescription
Public eventLogEventDropped
Occurs when LogEvent has been dropped.
Top
Remarks
Examples

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

XML
 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>

To set up the log target programmatically use code like this:

C#
 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.

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

See Also