using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace FastGithub.ReverseProxy
{
///
/// 请求日志中间件
///
sealed class RequestLoggingMilldeware
{
private readonly ILogger logger;
///
/// 请求日志中间件
///
///
public RequestLoggingMilldeware(ILogger logger)
{
this.logger = logger;
}
///
/// 执行请求
///
///
///
///
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
try
{
await next(context);
}
finally
{
stopwatch.Stop();
}
var request = context.Request;
var response = context.Response;
var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
var exception = context.GetForwarderErrorFeature()?.Exception;
if (exception == null)
{
this.logger.LogInformation(message);
}
else
{
this.logger.LogError($"{message}{Environment.NewLine}{exception.Message}");
}
}
}
}