-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Memory target
Rolf Kristensen edited this page Dec 2, 2023
·
21 revisions
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;
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json