One of the frequent feature requests I’ve been getting was to improve the way exceptions are logged. Recent builds of NLog 2.0 include several usability enhancements that should make working with exceptions much easier.
Conditional formatting
Conditional logging allows you to make your layouts somewhat more dynamic – you can include/exclude fields based on conditions, provide default values and so on. In order to achieve this, you have 3 new layout renderers at your disposal.
${onexception:INNER} - output
To render a layout when the exception is being logged, use ${onexception:INNER}, it will output INNER only when current log event includes an exception (in other words when it was emitted using any of the Logger.*Exception() methods. INNER can include other layouts, for example:
<targets><targetname="f"type="File"layout="${message}${onexception:EXCEPTION OCCURRED\:${exception:format=tostring}}"/></targets>
It will output log message, but in case of exception, it will also log detailed exception information prefixed with "EXCEPTION OCCURRED:".…