Search code examples
memcachedservicestacknlogenyim.caching

How to raise the minimum log level for specific libraries with NLog?


I'm using ServiceStack with Enyim.Memcached and NLog. My NLog configuration looks like this:

<nlog internalLogFile="C:\LogFiles\nlog.log" internalLogLevel="Warn">
    <targets>
        <target name="asyncLogFile" type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
            <target name="logFile" type="File" fileName="C:\LogFiles\application.log" layout="${date}|${level:uppercase=true}|${callsite}|${message}" />
        </target>
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" writeTo="asyncLogFile" />
    </rules>
</nlog>

All fine. However, Memcached writes a lot of diagnostics that I don't want in the log file, like:

2015/08/11 09:59:29.317|DEBUG|Enyim.Caching.Memcached.Protocol.Text.TextSocketHelper.ReadLine|ReadLine: END

I've tried adding this rule above the current one, but this has no effect:

<logger name="*Memcached*" minlevel="Warn" writeTo="asyncLogFile" final="true" />

I want to only write log messages from Memcached that have a Level of Warn or higher. What am I doing wrong? Also, the Enyim.Memcached code does a check on log.IsDebugEnabled before attempting to write to the log. If it's possible to change my configuration to do what I want, will this cause this property to be false?


Solution

  • Looks like it was an issue with the wildcard. I added ${logger} to the target's layout attribute to see the exact logger name producing the Memcached logs. Turns out there's quite a few. The main one is called Enyim.Caching.Memcached.MemcachedNode.InternalPoolImpl, but there's also TextSocketHelper and GetHelper (no namespaces on these).

    So adding these rules worked:

    <logger name="Enyim.Caching.*" minlevel="Warn" writeTo="asyncLogFile" final="true" />
    <logger name="TextSocketHelper" minlevel="Warn" writeTo="asyncLogFile" final="true" />
    <logger name="GetHelper" minlevel="Warn" writeTo="asyncLogFile" final="true" />
    

    This doesn't seem quite right, it would be nicer to be able to tell Enyim.Memcached to not log this stuff.

    If the minlevel for a logger is higher than "Debug", then the IsDebugEnabled property of the logger object will be false. So this configuration change will prevent the logging calls in the Enyim.Memcached library.