Search code examples
c#nlog

Access Memory Target in 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.


Solution

  • 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;