Asp.NET core logs each request that enters based on configuration. Now i'd like to have the same functionality for Flurl requests i sent. Most notably, I of course would like to know when a requests fails or does not complete. For debugging I found logging all requests in a verbose matter was extremely helpful.
Sure can. For cross-cutting concerns like logging, you want to use Flurl's event handlers, specifically BeforeCall
, AfterCall
, and OnError
. Here's an error logging example:
private async Task HandleFlurlErrorAsync(HttpCall call)
{
await LogErrorAsync(call.Exception.Message);
call.ExceptionHandled = true; // prevents exception from bubbling up, if desired
}
// Configure globally:
FlurlHttp.Clients.WithDefaults(builder =>
builder.BeforeCall(HandleFlurlErrorAsync));
The above example registers the handler for all Flurl calls when the clientless pattern is used. There are other options, such as configuring for a specific client or request, or registering it with your DI container. See the docs for more details.