Memory target - NLog/NLog GitHub Wiki
Writes log messages to an ArrayList in memory for programmatic retrieval.
Platforms Supported: All
<targets>
<target xsi:type="Memory" name="String" layout="Layout" />
</targets>
Read more about using the Configuration File.
- name - Name of the target.
-
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
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
var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
var logEvents = target.Logs;