Skip to content
Mattias Nordqvist edited this page Nov 20, 2017 · 3 revisions

Wanna log what's going in and out of web anchor?

public class LoggingHandler : DelegatingHandler
{
    public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
    {
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // Log your request. Use "await request.Content.ReadAsStringAsync().ConfigureAwait(false)" to read body content
        
        var response = await base.SendAsync(request, cancellationToken);

        // Log the response. Use "await response.Content.ReadAsStringAsync().ConfigureAwait(false)" to read body content
        return response;
    }
}

Then create your api like this

var handler = new HttpClientHandler();
var httpClient = new HttpClient(new LoggingHandler(handler));
httpClient.BaseAddress = new Uri(...);
return Api.For<IYourApi>(httpClient);

Documentation (v6.1.0)

  1. Getting started
  2. Basics (Http GET & Base Location)
  3. Basics pt. 2 (POST/PUT/DELETE, HttpResponseMessage, Exceptions, HttpClient, IDisposable)
  4. Building an HttpRequestMessage
  5. Parameter attributes
  6. Handling responses
  7. ApiSettings
  8. Typical setup (Develop/Test/Production & Mocking)
  9. Logging
  10. Testing
  11. Generic Api
  12. Multipart
Clone this wiki locally