Created
April 2, 2013 16:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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