Skip to content
Rolf Kristensen edited this page Dec 2, 2023 · 21 revisions

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

Platforms Supported: All

Configuration Syntax

<targets>
  <target xsi:type="Memory" name="String" layout="Layout" />
</targets>

Read more about using the Configuration File.

Parameters

General Options

  • name - Name of the target.

Layout Options

  • layout - Layout used to format log messages. Layout Required. Default: ${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}

  • MaxLogsCount Max count, will remove first if over this threshold. Default: 0 (No limit).

    Introduced with NLog v4.6

Examples

Setup MemoryTarget and enumerate messages

var config = new NLog.Config.LoggingConfiguration();
var memoryTarget = new NLog.Targets.MemoryTarget();
memoryTarget.Layout = "${message}";   // Message format
config.AddRuleForAllLevels(memoryTarget);
LogManager.Configuration = config;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");

foreach (string s in memoryTarget.Logs)
{
    Console.Write("logged: {0}", s);
}

See also Memory Target Tests

See also Memory Simple Example.cs

Reading from existing target

var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
var logEvents = target.Logs;
Clone this wiki locally