Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Memory Target in NLog

Tags:

c#

nlog

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.

like image 661
Zeus82 Avatar asked Jan 10 '23 13:01

Zeus82


2 Answers

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;
like image 135
Thomas Weller Avatar answered Jan 21 '23 10:01

Thomas Weller


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

like image 42
Xharze Avatar answered Jan 21 '23 10:01

Xharze