简体   繁体   中英

NLog memory target error

I have a Memory target and a File target in NLog set like this

<?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"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="true"
  internalLogLevel="Off" internalLogFile="D:\temp\nlog-internal.log">

<targets>
    <target name="logfile" xsi:type="File" fileName="file.txt" />
    <target name="memory" xsi:type="Memory"  layout="${level}|${time}|${message}"/>
</targets>

<rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
    <logger name="*" minlevel="Info" writeTo="memory" />
</rules>
</nlog>

I get the MemoryTarget in the constructor like this

target = (MemoryTarget)NLog.LogManager.Configuration.FindTargetByName("memory");

After that I access the target's logs (still in the constructor) and there are a couple of messages. But after an N amount of messages NLog stops writing any messages to the memory target. Even more, if you try to get the target by name like I do in the beggining it returns null. Also after some time and fiddling around nlog falls with an OutOfMemoryException. The file target works fine though. How can you fix the MemoryTarget not writing logs and deleting itself?

UPD: I looked at the NLog internal debug log and noticed that at some point the targets get wiped. I have my main program with an NLog config and a dll with another one. But they are the same and should not conflict with each other. The internal log is here http://pastebin.com/M885xmBi

So the problem was basically a conflict of NLog Configs. I had a project with functions with NLog in them and a wpf application that uses that project as a dll. Even though their configs were the same NLog read the first config and set the targets, then it read the second one but apperently failed (I don't know, there is no exception in NLog inner logs) and deleted all the targets. Removing the config from the dll project fixed the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM