title: Asp.net core middleware routing delay between request and action
body: <p>I am working on a rest API .net core 2.0 which takes JSON payload of around 1MB. While checking logs, I find a huge delay between request to action method invoking.</p>
  
  <p>Logs:</p>
  
  <blockquote>
    <p><strong>20:05:12.7247</strong>|1|INFO|Microsoft.AspNetCore.Hosting.Internal.WebHost|Request starting HTTP/1.1 POST </p>
    
    <p><strong>20:05:12.7514</strong>||INFO|Service.RequestLoggingMiddleware|Middleware Request recieved at 2019-03-12 20:05:12.725<br>
    <strong>20:05:15.2839</strong>|1|INFO|Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|Executing
    action method</p>
  </blockquote>
  
  <p>Around 2 seconds delay between request received and routed to the corresponding action. 
  I have added only Nlog provider, custom middleware to log request reception and mvc in middleware pipeline.</p>
  
  <p>Custom Middleware logging time of request:</p>
  
  <pre><code> public class RequestLoggingMiddleware
   {
      private readonly RequestDelegate requestDelegate;
      private ILogger&lt;RequestLoggingMiddleware&gt; logger;
      public RequestLoggingMiddleware(RequestDelegate _requestDelegate,ILogger&lt;RequestLoggingMiddleware&gt; _logger)
      {
          requestDelegate = _requestDelegate;
          logger = _logger;
      }
  
      public async Task Invoke(HttpContext context)
      {
          try {
  
  
              string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff",CultureInfo.InvariantCulture);
              logger.LogInformation("Middleware Request recieved at {0}", timestamp);
  
              await requestDelegate.Invoke(context);
          }
          catch { }
      }
  
  
  }
  </code></pre>
  
  <p>Middeware pipeline configured:</p>
  
  <pre><code>    public void Configure(IApplicationBuilder app, IHostingEnvironment 
       env,ILoggerFactory loggerFactory)
  
          {
  
           if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          env.ConfigureNLog("nlog.config");
          loggerFactory.AddNLog();
          app.AddNLogWeb();
          app.UseMiddleware&lt;RequestLoggingMiddleware&gt;();
          app.UseMvc();
         }
  </code></pre>
  
  <p>Edit: I find delay is proportional to size of Json payload.</p>
  
  <pre><code> public SampleController(ILogger&lt;GroupsValidation1Controller&gt; _logger)
   {
          logger = _logger;
   }
  
         [HttpPost]
   public   IActionResult Validate([FromBody]JObject inputValue)
   {
        logger.LogInformation(" method starts ");
      ///controllerlogic
       logger.LogInformation(" method ends");
   }
  </code></pre>
  
  <p>Why .Net Core MVC middleware is taking this much time?
   Is there any way to reduce delay spent in middleware?<br>
   I also find entire request is also logged though it is not configured. I am not sure if logging entire request is taking time. Is there any way to disable it?</p>