Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RequestExecutionMiddleware
- {
- private readonly RequestDelegate _next;
- public RequestExecutionMiddleware(RequestDelegate next)
- {
- _next = next;
- }
- public async Task Invoke(HttpContext httpContext)
- {
- var watch = new Stopwatch();
- httpContext.Response.OnStarting(() =>
- {
- watch.Stop();
- // here you can log it where you want.
- httpContext
- .Response
- .Headers
- .Add("X-Processing-Time-Milliseconds",
- new[] { watch.ElapsedMilliseconds.ToString() });
- return Task.CompletedTask;
- });
- watch.Start();
- await _next(httpContext);
- }
- }
- public static class CustomMiddlewareExtensions
- {
- public static IApplicationBuilder UseRequestExecutionMiddleware(this IApplicationBuilder builder)
- {
- return builder.UseMiddleware<RequestExecutionMiddleware>();
- }
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
- loggerFactory.AddDebug();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseDatabaseErrorPage();
- }
- app.UseStaticFiles();
- app.UseRequestExecutionMiddleware();
- app.UseSession();
- app.UseMvc(route =>
- {
- route.MapRoute(
- name: "Default",
- template: "{controller=Home}/{action=Index}/{Id?}"
- );
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement