Last updated: 2006-09-18

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

MSMQ Target


Writes log message to the specified message queue handled by MSMQ.

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

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.
queuestring  ${}
Name of the queue to write to.

To write to a private queue on a local machine use .\private$\QueueName. For other available queue names, consult MSMQ documentation.

createQueueIfNotExistsboolean
Create the queue if it doesn't exists.

Default value is: False.

encodingstring
Encoding to be used when writing text to the queue.
labelstring  ${}
The label to associate with each message.

Default value is: NLog.

By default no label is associated.

recoverableboolean
Use recoverable messages (with guaranteed delivery).

Default value is: False.

useXmlEncodingboolean
Use the XML format when serializing message.

Default value is: False.

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="queue" xsi:type="MSMQ" layout="${message}" 
            queue=".\private$\nlog" encoding="iso-8859-2" recoverable="true" 
            label="${logger}"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="queue"/> 
  </rules> 
</nlog> 

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

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="queue" xsi:type="MSMQ" layout="${message}" 
            queue=".\private$\nlog.${logger}" encoding="iso-8859-2" 
            recoverable="true" label="${logger}"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="queue"/> 
  </rules> 
</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:

using NLog; 
using NLog.Config; 
using NLog.Win32.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        NLog.Internal.InternalLogger.LogToConsole = true; 
 
        MSMQTarget target = new MSMQTarget(); 
        target.Queue = ".\\private$\\nlog"; 
        target.Label = "${message}"; 
        target.Layout = "${message}"; 
        target.CreateQueueIfNotExists = true; 
        target.Recoverable = true; 
 
        SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
        Logger l = LogManager.GetLogger("AAA"); 
        l.Error("This is an error. It goes to .\\private$\\nlog queue."); 
        l.Debug("This is a debug information. It goes to .\\private$\\nlog queue."); 
        l.Info("This is a information. It goes to .\\private$\\nlog queue."); 
        l.Warn("This is a warn information. It goes to .\\private$\\nlog queue."); 
        l.Fatal("This is a fatal information. It goes to .\\private$\\nlog queue."); 
        l.Trace("This is a trace information. It goes to .\\private$\\nlog queue."); 
    } 
}

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