Search code examples
c#web-servicesasynchronousasmxnlog

Why NLogTraceListener ignores nlog async wrapper?


I use NLog for loging asmx service events. Part of web.config:

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
        <source name="System.Web.Services.Asmx">
            <listeners>
                <add name="nlog" type="NLog.NLogTraceListener, NLog" />
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="System.Web.Services.Asmx" value="All" />
    </switches>
</system.diagnostics>

I use asyncWrapper in my nlog.config:

<targets>
    <target name="AsyncTarget" xsi:type="AsyncWrapper">
        <target xsi:type="File" fileName="//192.168.0.5/Logs/file.log" layout="${message}"/>
    </target>
</targets>

<rules>
    <logger name="*" writeTo="AsyncTarget"/>
</rules>

All logs are logged successfully. 10 requests (simulated at the same time) takes about 1 second.

The problem is when the log target IP is unreachable. Then the 10 requests (simulated at the same tame) takes about 10 second.

It seems like async wrapper is not working.

The problem is gone when I change NLog.NLogTraceListener listener to System.Diagnostics.TextWriterTraceListener in my web.config. Then the 10 request takes about 1 second (when the log target IP is unreachable). But I need to use NLog.

The problem is gone when I disable asmx loging from web.config an log only with nlog methods in code, then 10 requests takes about 1 second (when the log target IP is unreachable) and async wrapper works. But I need to use asmx source log.

Do you have any ideas how to solve this problem?


Solution

  • The solution is to add the disableFlush-setting:

    <add name="nlog" type="NLog.NLogTraceListener, NLog" disableFlush="true" />
    

    The current default value in NLog 4.4 is a shotgun ready to shoot yourself in the foot, and will be changed with NLog 4.5