Skip to content

Memory Leak with async target and usage of ${threadid}, GDC or MLDC in the layout. #2933

@staufour

Description

@staufour

NLog version: 4.5.10

Platform: .Net 4.5

Current NLog config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets async="true">
    <target name="logfile" xsi:type="File" fileName="log.txt">
      <layout xsi:type="CsvLayout">
        <withHeader>true</withHeader>
        <column name="Date" layout="${date:universalTime=True:format=o}" />
        <column name="Server" layout="${machinename}" />
        <column name="Logger" layout="${logger}" />
        <column name="Source" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="Message" layout="${message}" />
        <column name="Exception" layout="${exception:format=ToString}" />
      </layout>
    </target>
    <target name="logfile_crash" xsi:type="File" fileName="log_crash.txt">
      <layout xsi:type="CsvLayout">
        <withHeader>true</withHeader>
        <column name="Date" layout="${date:universalTime=True:format=o}" />
        <column name="Server" layout="${machinename}" />
        <!--<column name="ThreadId" layout="${threadid}" />-->
        <column name="Application" layout="${mdlc:item=Application}" />
        <!--<column name="Version" layout="${gdc:item=Version}" />-->
        <column name="Logger" layout="${logger}" />
        <column name="Source" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="Message" layout="${message}" />
        <column name="Exception" layout="${exception:format=ToString}" />
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="PerfTest" minlevel="Info" writeTo="logfile" />
    <logger name="PerfTestCrash" minlevel="Info" writeTo="logfile_crash" />
  </rules>
</nlog>

Hi,

We encounter a memory leak in our application in production, and the exploitation of memory dump led us to suspect NLog.
After many tests, we realized that using an asynchronous target with a ${threadid}, GDC or MLDC in the layout causes the leak.
The current thread hang, the memory increases and the application finally crash.

We can reproduce quickly by logging large messages quickly.

You can find a test project here with the demonstration : https://github.com/staufour/LoggerPerfTest

Thanks for your help.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions