NLog 使用 When Condition Renderer 获取值可用性

NLog using When Condition Renderer for value availability

我有一个日志输入值 (event-properties:item=Id),但该值有时可能为空。如果它是 null/empty,我希望 NLog 生成一个值 (guid)。我正在尝试这个 When Condition Renderer 但我无法让它生成任何东西,输入值仍然是空的。我做错了什么?

<parameter name="Id" type="System.Guid" 
layout="${when:when=length('${event-properties:item=Id}')>0:${event-properties\:item=Id}:${guid\:format=N}}"/>

使用 NLog 4.5。

备选方案:

<parameter name="Id" type="System.Guid" layout="${event-properties:Id:whenEmpty=${guid:format=N}}" />

顺便说一句。如果写入多个目标,您可能会考虑使用 GeneratedFromLogEvent 作为 ${guid}https://github.com/NLog/NLog/wiki/Guid-layout-renderer