-
Notifications
You must be signed in to change notification settings - Fork 8
Logging
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);
- Getting started
- Basics (Http GET & Base Location)
- Basics pt. 2 (POST/PUT/DELETE, HttpResponseMessage, Exceptions, HttpClient, IDisposable)
- Building an HttpRequestMessage
- Parameter attributes
- Handling responses
- ApiSettings
- Typical setup (Develop/Test/Production & Mocking)
- Logging
- Testing
- Generic Api
- Multipart