Improve your productivity - log clean, easy to understand exception stack traces using @benaadams's Demystifier.
Install from NuGet:
Install-Package NLog.DemystifiedExceptions
You should not need to make any changes to the NLog
configuration file for your application. The package will override NLog
's built-in ${exception}
layout renderer automatically.
NLog
should automatically detect and load the NLog.DemystifiedExceptions.dll
assembly as required.
If you have a scenario where this does not happen, you may have to register the package manually:
using NLog.LayoutRenderers;
LayoutRenderer.Register<DemystifiedExceptionLayoutRenderer>("exception");
Using the following code to log an exception will cause the exception to be logged twice:
logger.Error(ex);
This happens because NLog
will use the original exception stack trace for ${message}
and the demystified version for ${exception}
when you have this kind of configuration:
<targets>
<target xsi:type="ColoredConsole" name="console" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring} "/>
</targets>
To avoid the duplicates, always pass a message string, even if it is empty:
logger.Error(ex, string.Empty);
Implementation based on discussions in the NLog issue tracker: NLog/NLog#2159
With thanks to everybody involved!