-
-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to enable logging? #258
Comments
you should use a |
Aha, thanks, i'll check it |
var httpClient = new HttpClient(new HttpLoggingHandler()){ BaseAddress = /**/};
var contract = Refit.RestService.For<IGitApiContract>(httpClient);
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace Service.Shared.Clients
{
public class HttpLoggingHandler : DelegatingHandler
{
public HttpLoggingHandler(HttpMessageHandler innerHandler = null)
: base(innerHandler ?? new HttpClientHandler())
{ }
async protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var req = request;
var id = Guid.NewGuid().ToString();
var msg = $"[{id} - Request]";
Debug.WriteLine($"{msg}========Start==========");
Debug.WriteLine($"{msg} {req.Method} {req.RequestUri.PathAndQuery} {req.RequestUri.Scheme}/{req.Version}");
Debug.WriteLine($"{msg} Host: {req.RequestUri.Scheme}://{req.RequestUri.Host}");
foreach (var header in req.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (req.Content != null)
{
foreach (var header in req.Content.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (req.Content is StringContent || this.IsTextBasedContentType(req.Headers) || this.IsTextBasedContentType(req.Content.Headers))
{
var result = await req.Content.ReadAsStringAsync();
Debug.WriteLine($"{msg} Content:");
Debug.WriteLine($"{msg} {string.Join("", result.Cast<char>().Take(255))}...");
}
}
var start = DateTime.Now;
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
var end = DateTime.Now;
Debug.WriteLine($"{msg} Duration: {end - start}");
Debug.WriteLine($"{msg}==========End==========");
msg = $"[{id} - Response]";
Debug.WriteLine($"{msg}=========Start=========");
var resp = response;
Debug.WriteLine($"{msg} {req.RequestUri.Scheme.ToUpper()}/{resp.Version} {(int)resp.StatusCode} {resp.ReasonPhrase}");
foreach (var header in resp.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content != null)
{
foreach (var header in resp.Content.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content is StringContent || this.IsTextBasedContentType(resp.Headers) || this.IsTextBasedContentType(resp.Content.Headers))
{
start = DateTime.Now;
var result = await resp.Content.ReadAsStringAsync();
end = DateTime.Now;
Debug.WriteLine($"{msg} Content:");
Debug.WriteLine($"{msg} {string.Join("", result.Cast<char>().Take(255))}...");
Debug.WriteLine($"{msg} Duration: {end - start}");
}
}
Debug.WriteLine($"{msg}==========End==========");
return response;
}
readonly string[] types = new[] { "html", "text", "xml", "json", "txt", "x-www-form-urlencoded" };
bool IsTextBasedContentType(HttpHeaders headers)
{
IEnumerable<string> values;
if (!headers.TryGetValues("Content-Type", out values))
return false;
var header = string.Join(" ", values).ToLowerInvariant();
return types.Any(t => header.Contains(t));
}
}
} Gets Log as following:
|
Works like a charmed! Can i use it in pair with NativeMessageHandler? Also, Resharper recommends to change and |
Ofcourse you can, but i recommend using this handler only when debugging(#if DEBUG). and Oh, Who am i to disagree with R#? lol On Tue, 30 Aug 2016 at 18:44 Nearga notifications@github.com wrote:
|
Added support for |
Thanks @dbacinski i updated my comment to include "x-www-form-urlencoded" |
It would be nice if we could make this like a little optional helper to the library. |
It would be great to be able to see what is sent and what is received. Get requests works fine for me, however Posts are throwing 505 or 404 exceptions - taking a pick behind the curtains would help a lot.
The text was updated successfully, but these errors were encountered: