title: WCF service logging with IDispatchMessageInspector
body: <p>I'm trying to log the requests &amp; responses to &amp; from my WCF service. What I have done till now is:</p>
  
  <pre><code>public class OutputMessageInspector : IDispatchMessageInspector
  {
      int lastLogId;
  
     public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
     {
         // Parse request &amp; read required information
         // Insert request data into log tables
         // Set lastLogId to the id created above
     }
  
     public void BeforeSendReply(ref Message reply, object correlationState)
     {
         // Parse reply
         // Using lastLogId update the response column in database table
     }
  }
  </code></pre>
  
  <p>Everything is working fine but I have one concern:</p>
  
  <p><code>AfterReceiveRequest</code> and <code>BeforeSendReply</code> has to be working in a sync so that <code>BeforeSendReply</code> will update correct record. The case I have in mind is of Service getting called from multiple clients at the same time, question:</p>
  
  <ul>
  <li>Wouldn't <code>lastLogId</code> get messed up and shuffled among multiple
  requests &amp; responses?</li>
  <li>Would this logging specially update at <code>BeforeSendReply</code> will work
     fine with multiple clients calling service at the same time? If yes
     then please give me an explanation to assure my mind if no then
     please provide a better solution to this.</li>
  </ul>