44 lines
1.0 KiB
C#
44 lines
1.0 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Extensions
|
|
{
|
|
public class LogMiddleware
|
|
{
|
|
private readonly RequestDelegate next;
|
|
private readonly ILogger logger;
|
|
|
|
public LogMiddleware(RequestDelegate next, ILoggerFactory factory)
|
|
{
|
|
this.next = next;
|
|
logger = factory.CreateLogger<LogMiddleware>();
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
{
|
|
try
|
|
{
|
|
await next(context);
|
|
}
|
|
finally
|
|
{
|
|
logger.LogInformation("Request {method} {url} => {statusCode}",
|
|
context.Request?.Method,
|
|
context.Request?.Path.Value,
|
|
context.Response?.StatusCode);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class IApplicationBuilderExtensions
|
|
{
|
|
public static IApplicationBuilder UseRequestResponseLogging(this IApplicationBuilder builder)
|
|
{
|
|
builder.UseMiddleware<LogMiddleware>();
|
|
return builder;
|
|
}
|
|
}
|
|
}
|