title: Ensuring that a call is made to end a chain of methods
body: <p>Note/Disclaimer: After a few searches, the nearest thing I have I have seen to this post is a post on SO (<a href="https://stackoverflow.com/questions/2146623/method-chaining-and-the-finishing-problem">Method chaining and the finishing problem</a>) which is similar to my question, but doesn't really answer it - but anyway, I hope this is not a duplicate question.</p>
  
  <p><strong>What I am doing:</strong></p>
  
  <p>I have created a fluent interfaceas a facade over an existing logging framework for a bunch of method calls - so my syntax looks a bit like this:</p>
  
  <pre><code>Logger.Debug().Message("Debug message!").WriteToLog();
  Logger.Error().Message("An exception occured").Exception(ex).WriteToLog();
  </code></pre>
  
  <p>I am passing an internal object from one method call to the next object so that when the final call is made (the WriteToLog method); the message is written to a log file somewhere.</p>
  
  <p><strong>The bit I think smells</strong></p>
  
  <p>In order to verify (only when the application is built in debug mode), I have a property on a context class (just a property bag object) which gets passed from method call to the returned object until the chain terminates; it is a boolean and defaults to false. </p>
  
  <p>This property is evaluated in the context class destructor using a Debug.Assert to determine if the final method to end the chain is called so any logging errors can be picked up during development.
  (the property, the code which sets the property and the destructor itself are all created in the context of a #if DEBUG pre-processor directive, so if it is built in release or if the symbol doesn't exist, the code will not get compiled.)</p>
  
  <p>I <em>know</em> using a destructor is bad in c#2.0 and above, and that I may not have access to properties because I believe there are no guarantees about the finalization order. This is why it only happens when built in Debug mode, and why I would like to get away from it.</p>
  
  <p>The reason I am trying to build an assertation in is because it is very easy to forget and end up writing code like</p>
  
  <pre><code>Logger.Debug().Message("Debug message!");
  </code></pre>
  
  <p>which means that nothing gets logged, though at a cursory glance it looks like it should.</p>
  
  <p><strong>My Question</strong></p>
  
  <p>What I want to know is - can anyone think of another way of verifying that the final method is always called? These messages are just required during development to highlight to the developer that a method chain hasn't finished - I don't want the end users finding error messages related to logging in the end product.</p>