Lets say I have the following in my nlog.config (taken from http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_MemoryTarget.htm):
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="memory" xsi:type="Memory" layout="${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="memory" />
</rules>
</nlog>
How do I access this target programatically? I am trying to display the logs in a text box.
Exactly the same issue here, this worked for me:
var target =(MemoryTarget)LogManager.Configuration.FindTargetByName("memory");
var log = string.Join("\r\n", target.Logs);
txtLog.Text = log;
You can use LoggingConfiguration.FindTargetByName passing in the name of the target, then cast it to MemoryTarget, and use the Log property to get the logs gathered
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With