Log Request #1441
-
|
How do you enable logging of the request body that Refit sends? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
You can use HttpTracer as a DelegatingHandler or maybe Apizr to get a lot more features |
Beta Was this translation helpful? Give feedback.
-
|
Refit has no built-in request logging. Logging is the job of the HttpClient pipeline, so add a DelegatingHandler that logs the request (and body) and plug it in. Minimal handler: class LoggingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken ct)
{
if (request.Content != null)
{
var body = await request.Content.ReadAsStringAsync(ct);
Console.WriteLine($"{request.Method} {request.RequestUri}\n{body}");
}
return await base.SendAsync(request, ct);
}
}Wire it up with HttpClientFactory: services.AddRefitClient<IMyApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"))
.AddHttpMessageHandler(() => new LoggingHandler());Or without DI: var client = new HttpClient(new LoggingHandler { InnerHandler = new HttpClientHandler() })
{
BaseAddress = new Uri("https://api.example.com")
};
var api = RestService.For<IMyApi>(client);Prebuilt options if you do not want to roll your own: HttpTracer or Apizr (as suggested above) both work as the DelegatingHandler. |
Beta Was this translation helpful? Give feedback.
Refit has no built-in request logging. Logging is the job of the HttpClient pipeline, so add a DelegatingHandler that logs the request (and body) and plug it in.
Minimal handler:
Wire it up with HttpClientFactory: