title: How to log exceptions with network targets in NLog
body: <p>I am using the <a href="http://nlog-project.org" rel="nofollow noreferrer">NLog</a> logging framework and am trying to get exception and stacktrace information showing up in any UDP logger appliaction, such as <a href="http://sentinel.codeplex.com/" rel="nofollow noreferrer">Sentinel</a> and <a href="http://log2console.codeplex.com/" rel="nofollow noreferrer">Log2Console</a>, but can only get the log message part displayed. Outputting to a file works well as most examples do just that, so the problem revolves around using network targets with NLog.</p>
  
  <p>Bonus if a custom format can be applied on inner exceptions and stacktrace, but this is not required. Exception.ToString() would go a long way.</p>
  
  <p>Note on the example code: With <a href="http://log2console.codeplex.com/" rel="nofollow noreferrer">Log2Console</a> I found an <a href="http://nlog-forum.1685105.n2.nabble.com/How-to-send-stacktrace-of-exceptions-to-Chainsaw-or-Log2Console-td5465045.html" rel="nofollow noreferrer">article</a> on how to send exception as a separate log entry. Although this worked, I was not happy with the solution.</p>
  
  <p><strong>Example exception logging code:</strong></p>
  
  <pre><code>Logger Log = LogManager.GetCurrentClassLogger();
  
  try
  {
      throw new InvalidOperationException("My ex", new FileNotFoundException("My inner ex1", new AccessViolationException("Innermost ex")));
  }
  catch (Exception e)
  {
      Log.ErrorException("TEST", e);
  }
  </code></pre>
  
  <p><strong>Example NLog.config:</strong></p>
  
  <pre><code>&lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  
  &lt;targets async="true"&gt;
  
      &lt;!-- Send by UDP to Sentinel with NLogViewer protocol --&gt;
      &lt;target name="network" xsi:type="NLogViewer" address="udp://192.168.1.3:9999" layout="${message}${onexception:inner=${newline}${exception:format=tostring}}" /&gt;
  
      &lt;!-- Send message by UDP to Log2Console with Chainsaw protocol --&gt;
      &lt;target name="network2" xsi:type="Chainsaw" address="udp://192.168.1.3:9998" appinfo="Grocelist"/&gt;
  
      &lt;!-- Send exception/stacktrace by UDP to Log2Console with generic network protocol --&gt;
      &lt;target name="network2ex" xsi:type="Network" address="udp4://192.168.1.3:9998" layout="${exception:format=ToString}" /&gt;
  
      &lt;target name="logfile" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}"
                  createDirs="true"
                  fileName="${basedir}/logs/${shortdate}.log"
                  /&gt;
  &lt;/targets&gt;
  
    &lt;rules&gt;
      &lt;logger name="*" minlevel="Debug" writeTo="logfile" /&gt;
      &lt;logger name="*" minlevel="Debug" writeTo="network" /&gt;
      &lt;logger name="*" minlevel="Debug" writeTo="network2" /&gt;
      &lt;logger name="*" minlevel="Warn" writeTo="network2ex" /&gt;
    &lt;/rules&gt;
  &lt;/nlog&gt;
  </code></pre>
  
  <p><strong>Some links:</strong></p>
  
  <ul>
  <li><a href="http://nlog-project.org" rel="nofollow noreferrer">http://nlog-project.org</a></li>
  <li><a href="http://nlog-project.org/wiki/Targets" rel="nofollow noreferrer">http://nlog-project.org/wiki/Targets</a></li>
  <li><a href="http://nlog-project.org/wiki/Exception_layout_renderer" rel="nofollow noreferrer">http://nlog-project.org/wiki/Exception_layout_renderer</a></li>
  <li><a href="http://nlog-project.org/2011/04/20/exception-logging-enhancements.html" rel="nofollow noreferrer">http://nlog-project.org/2011/04/20/exception-logging-enhancements.html</a></li>
  <li><a href="http://nlog-project.org/wiki/How_to_properly_log_exceptions%3F" rel="nofollow noreferrer">http://nlog-project.org/wiki/How_to_properly_log_exceptions%3F</a></li>
  <li><a href="https://stackoverflow.com/questions/9199073/how-to-tell-nlog-to-log-exceptions">How to tell NLog to log exceptions?</a></li>
  <li><a href="https://stackoverflow.com/a/9684111/134761">https://stackoverflow.com/a/9684111/134761</a></li>
  <li><a href="http://nlog-forum.1685105.n2.nabble.com/How-to-send-stacktrace-of-exceptions-to-Chainsaw-or-Log2Console-td5465045.html" rel="nofollow noreferrer">http://nlog-forum.1685105.n2.nabble.com/How-to-send-stacktrace-of-exceptions-to-Chainsaw-or-Log2Console-td5465045.html</a></li>
  </ul>
  
  <p><strong>Edit:</strong>
  After searching some more this seems to be a limitation on NLog's end. A recent patch is apparently out there: <a href="http://nlog.codeplex.com/workitem/6777" rel="nofollow noreferrer">log4jxmlevent does not render Exception</a> </p>
  
  <p><strong>Edit2:</strong>
  I rebuilt NLog with patch, but it did not seem to help in Sentinel or Log2Console apps. I might have to try log4net to make sure those apps really do support what I am trying to achieve.</p>
  
  <p><strong>Edit3:</strong>
  I currently use string.Format() to join and format message and exception text myself. This works well, but is not what I'm looking for here.</p>