Reputation: 1016
I have a class with an NLog MemoryTarget. I have the need for each instance of the class that the logs from that instance goes to its MemoryTarget aggregated instance.
I use SimpleConfigurator.ConfigureForTargetLogging(instance.MemoryTarget, LogLevel.Trace) to initialise the logger, which causes the last ConfigureForTargetLogging call on the last instance to direct all logging to the last instance's MemoryTarget.
The MemoryTarget can be instantiated with a name, but I'm not sure I have a way to create a logger instance that sends to that named target only.
Upvotes: 1
Views: 979
Reputation: 1734
You can use logger name filters in rules: Do not forger final keyword that quit processing any further rule when this one matches:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="perf" xsi:type="File" fileName="perf.log" />
<target name="console" xsi:type="Console" />
</targets>
<rules>
<logger name="Perf" minlevel="Info" writeTo="perf.log" final="true" />
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>
Programmatically:
var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget() { FileName = "perf.log", Name = "perf" };
var console = new NLog.Targets.ConsoleTarget() { Name = "console" };
config.LoggingRules.Add(new NLog.Config.LoggingRule("Perf", LogLevel.Info, perf)){Final="true"};
config.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Debug, console));
NLog.LogManager.Configuration = config;
in your code you can get named logger:
Logger logger = NLog.LogManager.GetLogger("Perf")
Upvotes: 1