使用运算符“<”时 NLog 中断

NLog breaks when using operator '<'

我有下面的配置,关系运算符“<”似乎没有转义,因为它输出错误 XML element is not closedIn documentation 他们有一个带有“<=”的示例,但它也不起作用。有趣的是,'>' 工作正常。我错过了什么吗?

<logger name="*" minlevel="Trace" writeTo="logFile">
  <filters>
    <when condition="contains('${logger}','Domain.Messaging.') and level < LogLevel.Warn" action="Ignore" />
  </filters>
</logger>

Am I missing something?

是的,您没有考虑到这是 XML,其中 < 需要转义。你想要:

<logger name="*" minlevel="Trace" writeTo="logFile">
  <filters>
    <when condition="contains('${logger}','Domain.Messaging.') and level &lt; LogLevel.Warn" action="Ignore" />
  </filters>
</logger>

来自 XML 1.0 specification 的第 2.4 节:

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings &amp; and &lt; respectively.