使用变量分解常见的 NLog 布局

Using variables to factorize common NLog layouts

我正在使用 NLog 4.2.2。

是否可以通过变量分解多个目标共有的布局?

我试过这个:

<variable name="messageLayout" value="${longdate:universalTime=True}Z..."/>

<target name="console" xsi:type="ColoredConsole" layout="${messageLayout}"/>

<target name="messagesLogFile" xsi:type="File" layout="${messageLayout}"/>

不幸的是,这会在启动时触发以下异常:

An unhandled exception of type 'System.TypeInitializationException' occurred in Microsoft.VisualStudio.HostingProcess.Utilities.dll

NLog 的文档在涉及变量时非常稀疏。有什么线索吗?

As found out by a NLog contributor,这是我的错误:不能在 <target>...</target> 块内声明变量。在目标块之前移动变量声明可以解决问题。