title: C# MVC Logging method execution and performance
body: <p>There are many other posts regarding the recording of method execution time (for example through <a href="https://stackoverflow.com/questions/11805188/performance-monitoring-metrics-in-net-app/11806097#11806097">postsharp</a>, through <a href="https://stackoverflow.com/questions/5821411/measuring-performance-of-asp-net-mvc-3">action filters</a>, or through a custom method attribute).</p>
  
  <p>So to record the time for a method to complete is relatively straightforward at this point.</p>
  
  <p>What I am looking to do however, is to get more fine-grained performance metrics on a per-request basis utilizing, for example, session id to track all operations that occured for a given request - and the time elapses for all of them, not just the parent (i.e. action controller) method.</p>
  
  <p>For example, I would like to be able to do something like:</p>
  
  <pre><code>namespace MvcApplication1.Controllers
  {
      public class ProductController : Controller
      {
          //record start of method 
          public ActionResult Index()
          {
              //record start of service1.method call
              var data = service1.method();
              //store total time of service1.method call
  
              //record start of db call
              var objects = db.select(obj).where(id=0)
              //store total time of db call
  
              return View();
          }
          //record total time of method
      }
  }
  </code></pre>
  
  <p>Ideally I want to link all of these operations (the parent method, the service call and the db call) together - the most likely candidate would be through the session id - but that means that each call would need access to the session id.</p>
  
  <p>From what I've read, the best way of accomplishing this would be to utilize a method attribute to record the parent performance time, and then some sort of custom library function to store the various timing of the calls (probably using nlog to record).</p>
  
  <p>What I am asking for are opinions on what the best way (if at all possible) to accomplish the above?</p>
  
  <p>Am I missing something with any third party libraries that exist - i.e. does Unity or Postsharp provide this functionality (or some other library)?</p>
  
  <p>Is it possible to link all of these records via the session id? For example, I don't see how to via postsharp (1) store individual method calls INSIDE the MVC action, and (2) to pass variables between calls.</p>