Skip to content

Instantly share code, notes, and snippets.

@pcibraro
Created April 2, 2013 16:47
Show Gist options
  • Save pcibraro/5293844 to your computer and use it in GitHub Desktop.
Save pcibraro/5293844 to your computer and use it in GitHub Desktop.
A logging handler for the HttpClient in ASP.NET Web API. It logs the body content to a file. That code can be replaced for some other stream.
public class LogginHandler : DelegatingHandler
{
public LogginHandler(HttpMessageHandler inner)
: base(inner)
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
using (var fs = new FileStream("c:\\temp\\log.txt", FileMode.Create, FileAccess.Write))
{
await request.Content.CopyToAsync(fs);
}
return await base.SendAsync(request, cancellationToken);
}
}
// It's invoked as follow,
HttpClient client = new HttpClient(new LogginHandler(new HttpClientHandler()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment