5

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.

3dGrabber
  • 4,710
  • 1
  • 34
  • 42
sommmen
  • 6,570
  • 2
  • 30
  • 51

1 Answers1

10

Sure can. For cross-cutting concerns like logging, you want to use Flurl's event handlers, specifically BeforeCall, AfterCall, OnError, and their async equivalents (BeforeCallAsync, AfterCallAsync, OnErrorAsync). 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 once at startup:
FlurlHttp.Configure(settings => settings.OnErrorAsync = HandleFlurlErrorAsync);
3dGrabber
  • 4,710
  • 1
  • 34
  • 42
Todd Menier
  • 37,557
  • 17
  • 150
  • 173