[This is preliminary documentation and is subject to change.]
Writes log message to the specified message queue handled by MSMQ.
Namespace:
NLog.TargetsAssembly: NLog.Extended (in NLog.Extended.dll) Version: 2.0.1.0
Syntax
C# |
---|
public class MessageQueueTarget : TargetWithLayout |
Visual Basic (Declaration) |
---|
Public Class MessageQueueTarget _ Inherits TargetWithLayout |
Visual Basic (Usage) |
---|
Dim instance As MessageQueueTarget |
Examples
To set up the target in the configuration file, use the following syntax:

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

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>
The above examples assume just one target and a single rule. More configuration options are described here.
To set up the log target programmatically use code like this:

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}
Inheritance Hierarchy
Object
NLog.Targets..::.Target
NLog.Targets..::.TargetWithLayout
NLog.Targets..::.MessageQueueTarget
NLog.Targets..::.Target
NLog.Targets..::.TargetWithLayout
NLog.Targets..::.MessageQueueTarget