Skip to content

Instantly share code, notes, and snippets.

@StephanyBatista
Last active May 9, 2016 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephanyBatista/4bdd053ff1ff4a1a3bf3bf0dd799b806 to your computer and use it in GitHub Desktop.
Save StephanyBatista/4bdd053ff1ff4a1a3bf3bf0dd799b806 to your computer and use it in GitHub Desktop.
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace MiddlewareArticle.Middleware
{
public class ResponseTime
{
RequestDelegate _next;
public ResponseTime(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var sw = new Stopwatch();
sw.Start();
await _next(context);
var isHtml = context.Response.ContentType?.ToLower().Contains("text/html");
if (context.Response.StatusCode == 200 && isHtml.GetValueOrDefault())
{
var body = context.Response.Body;
using (var streamWriter = new StreamWriter(body))
{
var textHtml = string.Format(
"<footer><div id='process'>Response Time {0} milliseconds.</div>",
sw.ElapsedMilliseconds);
streamWriter.Write(textHtml);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment