Вы можете регистрировать всю информацию http-запроса в промежуточном программном обеспечении. Взгляните на пример ниже
1. Создать класс RequestHandlerMiddleware.cs
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;
namespace Onsolve.ONE.WebApi.Middlewares
{
public sealed class RequestHandlerMiddleware
{
private readonly RequestDelegate next;
private readonly ILogger logger;
public RequestHandlerMiddleware(ILogger<RequestHandlerMiddleware> logger, RequestDelegate next)
{
this.next = next;
this.logger = logger;
}
public async Task Invoke(HttpContext context)
{
logger.LogInformation($"Header: {JsonConvert.SerializeObject(context.Request.Headers, Formatting.Indented)}");
context.Request.EnableBuffering();
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
logger.LogInformation($"Body: {body}");
context.Request.Body.Position = 0;
logger.LogInformation($"Host: {context.Request.Host.Host}");
logger.LogInformation($"Client IP: {context.Connection.RemoteIpAddress}");
await next(context);
}
}
}
2.Добавить RequestHandlerMiddleware
в Configure
метод в Startup.cs
app.UseMiddleware<RequestHandlerMiddleware>();
или проще
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
{
app.Use(async (context, next) =>
{
logger.LogInformation($"Header: {JsonConvert.SerializeObject(context.Request.Headers, Formatting.Indented)}");
context.Request.EnableBuffering();
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
logger.LogInformation($"Body: {body}");
context.Request.Body.Position = 0;
logger.LogInformation($"Host: {context.Request.Host.Host}");
logger.LogInformation($"Client IP: {context.Connection.RemoteIpAddress}");
await next.Invoke();
});
}
Ссылка:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-2.2