Last active
February 13, 2020 03:11
-
-
Save guitarrapc/13eb3bc73f604e8d03a9b21c17694665 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using System.Threading.Tasks; | |
namespace LoggerSample | |
{ | |
public class RouteLoggingMiddleware | |
{ | |
private readonly RequestDelegate _next; | |
private readonly ILogger<RouteLoggingMiddleware> _logger; | |
public RouteLoggingMiddleware(RequestDelegate next, ILogger<RouteLoggingMiddleware> logger) | |
{ | |
_next = next; | |
_logger = logger; | |
} | |
public async Task InvokeAsync(HttpContext context) | |
{ | |
try | |
{ | |
await _next.Invoke(context); | |
} | |
finally | |
{ | |
_logger.LogInformation($"{context.Response.StatusCode}\t{context.Request.Host}\t{context.Request.Method}\t{context.Request.Path}"); | |
} | |
} | |
} | |
public static class RouteLoggingMiddlewareExtensions | |
{ | |
public static IApplicationBuilder UseRouteLogging(this IApplicationBuilder builder) | |
=> builder.UseMiddleware<RouteLoggingMiddleware>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment