Click or drag to resize

MessageQueueTarget Class

Writes log message to the specified message queue handled by MSMQ.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsMessageQueueTarget

Namespace:  NLog.Targets
Assembly:  NLog.MSMQ (in NLog.MSMQ.dll) Version: 5.3.1+40400fa5f822c8a012031d98e17482db9dd5fb65
Syntax
public class MessageQueueTarget : TargetWithLayout

The MessageQueueTarget type exposes the following members.

Constructors
  NameDescription
Public methodMessageQueueTarget
Initializes a new instance of the MessageQueueTarget class.
Public methodMessageQueueTarget(String)
Initializes a new instance of the MessageQueueTarget class.
Top
Properties
  NameDescription
Public propertyCheckIfQueueExists
Gets or sets a value indicating whether to check if a queue exists before writing to it.
Public propertyCreateQueueIfNotExists
Gets or sets a value indicating whether to create the queue if it doesn't exists.
Public propertyEncoding
Gets or sets the encoding to be used when writing text to the queue.
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Public propertyLabel
Gets or sets the label to associate with each message.
Public propertyLayout
Gets or sets the layout used to format log messages.
(Inherited from TargetWithLayout.)
Protected propertyLoggingConfiguration
Gets the logging configuration this target is part of.
(Inherited from Target.)
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyQueue
Gets or sets the name of the queue to write to.
Public propertyRecoverable
Gets or sets a value indicating whether to use recoverable messages (with guaranteed delivery).
Public propertySingleTransaction
Gets or sets a value indicating whether sending to a transactional queue using single-transaction-style.
Protected propertySyncRoot
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.)
Public propertyUseXmlEncoding
Gets or sets a value indicating whether to use the XML format when serializing message.
Top
Methods
  NameDescription
Protected methodCloseTarget
Closes the target to release any initialized resources
(Inherited from Target.)
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
(Inherited from Target.)
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 methodPrepareMessage
Prepares a message to be sent to the message queue.
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)
Writes async log event to the log target.
(Inherited from Target.)
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 the specified logging event to a queue specified in the Queue parameter.
(Overrides TargetWrite(LogEventInfo).)
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
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
 5    <targets>
 6        <target name="queue" xsi:type="MSMQ"
 7            layout="${message}" 
 8            queue=".\private$\nlog"
 9            encoding="iso-8859-2"
10            recoverable="true"
11            label="${logger}" />
12    </targets>
13
14    <rules>
15        <logger name="*" minlevel="Debug" writeTo="queue" />
16    </rules>
17</nlog>

You can use a single target to write to multiple queues (similar to writing to multiple files with the File target).

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
 5    <targets>
 6        <target name="queue" xsi:type="MSMQ"
 7            layout="${message}" 
 8            queue=".\private$\nlog.${logger}"
 9            encoding="iso-8859-2"
10            recoverable="true"
11            label="${logger}" />
12    </targets>
13
14    <rules>
15        <logger name="*" minlevel="Debug" writeTo="queue" />
16    </rules>
17</nlog>

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

C#
 1using NLog;
 2using NLog.Config;
 3using NLog.Win32.Targets;
 4
 5class Example
 6{
 7    static void Main(string[] args)
 8    {
 9        NLog.Internal.InternalLogger.LogToConsole = true;
10
11        MSMQTarget target = new MSMQTarget();
12        target.Queue = ".\\private$\\nlog";
13        target.Label = "${message}";
14        target.Layout = "${message}";
15        target.CreateQueueIfNotExists = true;
16        target.Recoverable = true;
17
18        SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
19
20        Logger l = LogManager.GetLogger("AAA");
21        l.Error("This is an error. It goes to .\\private$\\nlog queue.");
22        l.Debug("This is a debug information. It goes to .\\private$\\nlog queue.");
23        l.Info("This is a information. It goes to .\\private$\\nlog queue.");
24        l.Warn("This is a warn information. It goes to .\\private$\\nlog queue.");
25        l.Fatal("This is a fatal information. It goes to .\\private$\\nlog queue.");
26        l.Trace("This is a trace information. It goes to .\\private$\\nlog queue.");
27    }
28}
See Also