title: Log EF query time to database
body: <p>I want to log EF query (or transaction) times back to the database so that I can monitor performance in a live system. Note I know that more than one query can happen within my UOW. That is fine, I just want to be able to tell what areas of the system are slowing down etc.</p>
  
  <p>What is the best way for me to do this? My initial thought was to do this in <code>UnitOfWork.Dispose()</code> so that every query would automatically be logged once completed. But this smells a bit to me because I'd have to call <code>Save()</code> to persist the information, but what if the caller didn't want to save?</p>
  
  <p>Is there another, better way I can automatically log the query time to the database?</p>
  
  <pre><code>protected virtual void Dispose(bool disposing)
  {
      if (this.logQueryToDatabase)
      {
          var timeTaken = DateTime.UtcNow - this.start;
          LogPerformance(this.callingFnName, timeTaken.TotalMilliseconds);
      }
  
      this.ctx.Dispose();
  }
  </code></pre>