title: Bubble up exception with custom properties and log with serilog
body: <p>I came across a situation where I thought it'd be good to rethrow an exception, appending additional information to it (for example in properties in a custom exception) that when caught further up the stack would be logged with the additional info in the form of properties of a message template in Serilog - the end goal being so I can then filter on those properties in Seq. This scenario might be a warning that I need to rethink my exception handling, but I thought I'd ask if this has been done before with Serilog? Recommended/discouraged?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Here's an example scenario:</p>
  
  <p>Using serilog and Seq we apply a "Department" property to a log so that a department can easily see all exceptions that are likely their responsibility.</p>
  
  <p>Now the app for this scenario basically does two things:</p>
  
  <ul>
  <li>Step 1: Query data.</li>
  <li>Step 2: If no exceptions in step 1, post it somewhere else.</li>
  </ul>
  
  <p>A few layers deep in step 1 there is a particular exception that I know is the responsibility of a particular department. So I'd like to apply a department property to this exception when it is logged but at the same time have it be caught further up the stack so that the exception prevents continuation of everything else. Also, I only want to log the exception once.</p>
  
  <p>My current approach is to define a custom exception with a dictionary that can hold properties for the log:</p>
  
  <pre><code>public class ExtPropertiesException : Exception
  {
      // Constructors here...
  
      /// &lt;summary&gt;
      /// Properties to log with the error message
      /// &lt;/summary&gt;
      public Dictionary&lt;string, object&gt; ExtProperties { get; set; }
  }
  </code></pre>
  
  <p>So when I catch an exception I know is a particular departments responsibility I rethrow it as ExtPropertiesException or as an exception that inherits it, attaching the original exception as the inner exception.</p>
  
  <p>Then back up the stack where the general steps are I've got a catch:</p>
  
  <pre><code>try
  {
      // Step 1
      // Step 2
  }
  catch (ExtPropertiesException ex)
  {
      if (ex.ExtProperties != null)
      {
          foreach (var prop in ex.ExtProperties)
          {
              logger = logger.ForContext(prop.Key, prop.Value);
          }
      }
      logger.Error(ex, ex.Message);
  }
  </code></pre>
  
  <p>This seems to be doing what I need, but it'd be great to know whether it's a best practice before repeating it elsewhere.</p>
  
  <p>I was initially doing the logs and applying the property at the time I catch the exception and then just returning false or null to indicate failure and abandon future steps, however I've found it difficult for unit testing as I can't determine the type of errors from outside the methods.</p>
  
  <p>Thanks</p>