Created
June 23, 2017 06:52
-
-
Save programmation/2b5b59cf4520e7bbd5ca3aaf45feed8f to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace MyNamespace | |
{ | |
public class LoggingHttpClientHandler : HttpClientHandler | |
{ | |
public static int RequestId = 0; | |
public LoggingHttpClientHandler() | |
{ | |
} | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
var requestId = Interlocked.Increment(ref RequestId); | |
var requestPrefix = $" [REQ {requestId}] "; | |
var requestUrl = request.RequestUri.ToString(); | |
this.Debug(requestPrefix + $"URL: {requestUrl}"); | |
this.Debug(requestPrefix + $"HDR:"); | |
foreach (var header in request.Headers) | |
{ | |
this.Debug(requestPrefix + $" {header.Key}: {String.Join(",", header.Value)}"); | |
} | |
this.Debug(requestPrefix + $"CKK:"); | |
foreach (var cookie in CookieContainer.GetCookies(request.RequestUri)) | |
{ | |
this.Debug(requestPrefix + $" {cookie.ToString()}"); | |
} | |
this.Debug(requestPrefix + $"PRP:"); | |
foreach (var propertyKey in request.Properties.Keys) | |
{ | |
this.Debug(requestPrefix + $" {propertyKey}: {request.Properties[propertyKey].ToString()}"); | |
} | |
if (request.Content != null) | |
{ | |
foreach (var header in request.Content.Headers) | |
{ | |
this.Debug(requestPrefix + $" {header.Key}: {String.Join(",", header.Value)}"); | |
} | |
this.Debug(requestPrefix + $"CNT:"); | |
this.Debug(requestPrefix + await request.Content.ReadAsStringAsync()); | |
} | |
var sendTime = DateTimeOffset.Now; | |
var response = await base.SendAsync(request, cancellationToken); | |
var receiveTime = DateTimeOffset.Now; | |
this.Debug(requestPrefix + $"TRN: {receiveTime - sendTime}s"); | |
var responsePrefix = $" [REP {requestId}] "; | |
this.Debug(responsePrefix + $"HDR:"); | |
foreach (var header in response.Headers) | |
{ | |
this.Debug(responsePrefix + $" {header.Key}: {String.Join(",", header.Value)}"); | |
} | |
if (response.Content != null) | |
{ | |
foreach (var header in response.Content.Headers) | |
{ | |
this.Debug(responsePrefix + $" {header.Key}: {String.Join(",", header.Value)}"); | |
} | |
this.Debug(responsePrefix + $"CNT:"); | |
if (response.Content.Headers.ContentType.IsTextTypeMedia()) | |
{ | |
this.Debug(responsePrefix + await response.Content.ReadAsStringAsync()); | |
} | |
else | |
{ | |
this.Debug(responsePrefix + " BINARY"); | |
} | |
} | |
return response; | |
} | |
} | |
public static class MediaTypeExtensions | |
{ | |
public static bool IsTextTypeMedia(this MediaTypeHeaderValue mediaTypeHeaderValue) | |
{ | |
var mediaType = mediaTypeHeaderValue.MediaType; | |
if (mediaType.StartsWith("text/", StringComparison.Ordinal)) | |
{ | |
return true; | |
} | |
if (mediaType == "application/json" || | |
mediaType.StartsWith("application.xhtml", StringComparison.Ordinal) || | |
mediaType.StartsWith("application.xml", StringComparison.Ordinal) | |
) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment