Nlog: Only one side of <when> layout renderer being evaluated

Created on 25 Jul 2018  路  3Comments  路  Source: NLog/NLog

NLog version: 4.5.7 (latest as of today).

Platform: .NET Core 2.1 (netcoreapp2.1).

Current NLog config (xml or C#, if relevant)

<?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">
  <targets>
    <target xsi:type="Console" name="Console" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${logger}|${message}${onexception:inner= ${exception:format=toString,Data}}" />
    <target xsi:type="File" name="ChatLogFile" fileName="${event-properties:item=ChatGroupID}-${event-properties:item=ChatID}${when:when='${event-properties:item=ChatGroupID}' == 0:inner=-${event-properties:item=SteamID}}.txt" layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${event-properties:item=Message} ${when:when='${event-properties:item=Echo}' == 'true':inner=-&gt;:else:&lt;-} ${event-properties:item=SteamID}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="Console" />
    <logger name="*" level="Trace" writeTo="ChatLogFile">
      <filters>
        <when condition="not starts-with('${message}','OnIncoming') and not starts-with('${message}','SendMessage')" action="Ignore" />
      </filters>
    </logger>
  </rules>
</nlog>

I provided full config I reproduced the issue with, most likely only ChatLogFile target/rules are relevant.

  • What is the current result?

Currently, log file is written like that:

0-0-76561198006963719.txt:

2018-07-25 23:44:59 !version  76561198006963719
2018-07-25 23:44:59 <1> ASF V3.3.0.0 <- 76561198006963719
  • What is the expected result?

The same file should be written like that:

0-0-76561198006963719.txt:

2018-07-25 23:44:59 !version -> 76561198006963719
2018-07-25 23:44:59 <1> ASF V3.3.0.0 <- 76561198006963719

Notice how we replaced null on the first line with ->.

Yes, the full log is available here. The only relevant parts to me are:

2018-07-25 23:46:46.4022 Debug Setting 'WhenLayoutRendererWrapper.Inner' to 'else'
2018-07-25 23:46:46.4062 Debug Setting 'WhenLayoutRendererWrapper.Inner' to '<-'

You'll most likely find more info in full log above.

  • Please post full exception details (message, stacktrace, inner exceptions)

Nothing, there seem to be no errors, it's just the fact that inner part of my when condition is rendered as null (I assume), while else part is rendered properly.

  • Are there any workarounds?

No, I didn't find any. I tried really a lot of things, including entirely different messages, changing conditions or different layouts - the inner part is never rendered.

  • Is there a version in which it did work?

Sorry, I did not try it in earlier versions.

  • Can you help us by writing an unit test?

I think it could be fairly easy with above reproducible case. Sorry, I'm not familiar with NLog code that much, so I'd prefer to not send a PR, but instead provide all details for you.

Extra info

I honestly believe this is a bug, but if by any chance I'm blind with some very obvious mistake such as wrong layout usage or anything, I'm sorry in advance and thank you for eventual correction. I don't see a reason why only one side of the if condition worked properly - I tried it with official <when> example and I had the same issue.

If it helps, this is how I'm rendering my messages from within C# code.

```c#
LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Trace, Logger.Name, loggedMessage.ToString()); // loggedMessage is StringBuilder, Logger is LogManager.GetLogger("name")
logEventInfo.Properties["Echo"] = echo; // bool
logEventInfo.Properties["Message"] = message; // string
logEventInfo.Properties["ChatGroupID"] = chatGroupID; // ulong
logEventInfo.Properties["ChatID"] = chatID; // ulong
logEventInfo.Properties["SteamID"] = steamID; // ulong

Logger.Log(logEventInfo);
```

Thank you in advance for taking a look.

documentation question

Most helpful comment

@JustArchi Have updated the Wiki-page

All 3 comments

You are missing an = for the else. It should be like this:

${when:when='${event-properties:item=Echo}' == 'true':inner=-&gt;:else=&lt;-}

Thank you @snakefoot! Then it's a wiki mixup, since in this example it's written as:

${when:when='${aspnet-request:serverVariable=HTTPS}' == 'on':inner=1:else:0}

I guess this should be corrected - since this issue is not a bug as I thought, I'm closing it. Thanks again!

@JustArchi Have updated the Wiki-page

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sszost picture sszost  路  3Comments

haythamabutair picture haythamabutair  路  3Comments

Sam13 picture Sam13  路  3Comments

npandrei picture npandrei  路  3Comments

imanushin picture imanushin  路  3Comments