[This is preliminary documentation and is subject to change.]

Writes log messages to an ArrayList in memory for programmatic retrieval.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public sealed class MemoryTarget : TargetWithLayout
Visual Basic (Declaration)
Public NotInheritable Class MemoryTarget _
	Inherits TargetWithLayout
Visual Basic (Usage)
Dim instance As MemoryTarget

Examples

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

CopyXML
 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="memory" xsi:type="Memory" layout="${message}" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Info" writeTo="memory" />
10    </rules>
11</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:

CopyC#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5
 6class Example
 7{
 8    static void Main(string[] args)
 9    {
10        MemoryTarget target = new MemoryTarget();
11        target.Layout = "${message}";
12
13        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
14
15        Logger logger = LogManager.GetLogger("Example");
16        logger.Debug("log message");
17
18        foreach (string s in target.Logs)
19        {
20            Console.Write("logged: {0}", s);
21        }
22    }
23}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.MemoryTarget

See Also