Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active February 13, 2020 03:11
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 guitarrapc/13eb3bc73f604e8d03a9b21c17694665 to your computer and use it in GitHub Desktop.
Save guitarrapc/13eb3bc73f604e8d03a9b21c17694665 to your computer and use it in GitHub Desktop.
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