Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Log all requests using Flurl.Http?

Tags:

flurl

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.

like image 313
sommmen Avatar asked Nov 21 '19 09:11

sommmen


People also ask

What does Flurl mean?

Flurl stands for the Fluent URL. Quoting Flurl's home page: Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library for . NET. It's simple as that.

What is FlurlClient?

FlurlClient is a lightweight wrapper around HttpClient and is tightly bound to its lifetime. It implements IDisposable , and when disposed will also dispose HttpClient . FlurlClient includes a BaseUrl property, as well as Headers , Settings , and many of the fluent methods you may already be familiar with.


1 Answers

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);
like image 135
Todd Menier Avatar answered Oct 09 '22 16:10

Todd Menier