1

I'm using NLog 4.2.2.

Is there a way to factorize layouts common to multiple targets, maybe with variables?

I've tried this:

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

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

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

Unfortunately this triggers the following exception at startup:

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

NLog's documentation is very sparse when it comes to variables. Any clue?

François Beaune
  • 4,270
  • 7
  • 41
  • 65
  • Can you post an entire NLog configuration section/file? – Anton Gogolev Dec 29 '15 at 16:55
  • @AntonGogolev Sure, here we go: https://gist.github.com/dictoon/2c91ca9553baebe2df8b – François Beaune Dec 29 '15 at 17:03
  • You should be able to do this. I have done it before and its shown here http://stackoverflow.com/questions/4091606/most-useful-nlog-configurations I would try simplifying your config to one of the basic examples provided in that post and add back in your pieces slowly. There might be something else in your config throwing that error (its a very general NLog failed to setup your logger, message). Not near my dev computer right now so I can't actually try out your config :( – Alex Bezek Dec 31 '15 at 16:04
  • @alexbezek Thanks for your input. Everything works fine when I replace `${messageLayout}` by its value. The conversation continues on GitHub: https://github.com/NLog/NLog/issues/1121#issuecomment-168320967 – François Beaune Jan 04 '16 at 10:56

1 Answers1

1

As found out by a NLog contributor, it was a mistake on my side: variables cannot be declared inside a <target>...</target> block. Moving the variable declaration before the target block fixes the problem.

François Beaune
  • 4,270
  • 7
  • 41
  • 65