title: .NET Core custom ILogger middleware to catch long running requests
body: <p>I am slowly learning .NET Core and am struggling to get my head around the logging middleware. I have written a custom ILogger which logs out to a text file. By default, .NET core will log Information about the length of time each request takes, for example:</p>
  
  <pre><code>Information: Request finished in 154.0077ms 200 application/json
  </code></pre>
  
  <p>I have a requirement to log any requests that take longer than <em>n</em> ms as a warning rather than information so that in production the default log level can be set to Warning and these slow requests are still captured</p>
  
  <p>At the moment I'm doing the following, which works, but only if the default log level is set to Information (which I don't want in production). Obviously changing the default to warning means that the "request finished" data doesn't even get passed to the logger.</p>
  
  <pre><code>Regex requestFinished = new Regex(@"(.*\s)([0-9]{1,}[.]?[0-9]{1,})([m]?s)");
  Match match = requestFinished.Match(message);
  
  if (match.Success)
  {
      double timeTaken = 0;
      Double.TryParse(match.Groups[2].ToString(), out timeTaken);
  
      if (timeTaken &gt; MyService.ExecutionThreshold)
      {
           logLevel = LogLevel.Warning;
      }
  }
  </code></pre>
  
  <p>I suspect there's a much easier way of doing this than the above but I cannot figure it out and can't seem to find any suggestions online. Any help gratefully received.</p>