Skip to content

Instantly share code, notes, and snippets.

@ebaxt
Created May 27, 2014 19:41
Show Gist options
  • Save ebaxt/68f2107216fe1dec56a0 to your computer and use it in GitHub Desktop.
Save ebaxt/68f2107216fe1dec56a0 to your computer and use it in GitHub Desktop.
LoggingHandler
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.WriteLine("Request:");
Debug.WriteLine(request.ToString());
if (request.Content != null)
{
Debug.WriteLine(await request.Content.ReadAsStringAsync());
}
Debug.WriteLine("");
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Debug.WriteLine("Response:");
Debug.WriteLine(response.ToString());
if (response.Content != null)
{
Debug.WriteLine(await response.Content.ReadAsStringAsync());
}
Debug.WriteLine("");
return response;
}
}
;; Usage
var client = new HttpClient(new LoggingHandler(new HttpClientHandler()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment