I'm using nLog 2.0 and after this interesting read I tried to apply conditions to a simple layout as such:
layout="${message:when=logger==A}"
layout="${message:when=logger=='A'}"
layout="${message:when='logger==A'}"
Not only do none of these have any effect, they also do not throw an error so it seems the condition is silently swallowed somewhere (throwExceptions
is set to true)
Here is the full code, this is a basic console application.
main.cs:
class Program
{
static void Main( string[] args )
{
NLog.LogManager.GetLogger( "A" ).Info( "from A" );
NLog.LogManager.GetLogger( "B" ).Info( "from B" );
}
}
NLog.config (in executable directory):
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true">
<targets>
<target name="main" xsi:type="File" fileName="my.log"
deleteOldFileOnStartup="true" keepFileOpen="true"
layout="${callsite} ${message:when=logger=='A'}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="main" />
</rules>
</nlog>
Output:
ConsoleApplication1.Program.Main from A -> this should only log ${callsite}
ConsoleApplication1.Program.Main from B
Thanks! Try to set logger name:
<logger name="A" minlevel="Trace" writeTo="main" />
<logger name="B" minlevel="Trace" writeTo="main" />
Now there are two loggers, they are available in the code - conditions should work.