Spotify.NET/SpotifyAPI.Web/Http/SimpleConsoleHTTPLogger.cs

37 lines
1.2 KiB
C#
Raw Normal View History

using System;
2022-11-27 12:29:35 +00:00
using System.Linq;
namespace SpotifyAPI.Web.Http
{
2020-05-30 23:11:05 +01:00
public class SimpleConsoleHTTPLogger : IHTTPLogger
{
private const string OnRequestFormat = "\n{0} {1} [{2}] {3}";
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303")]
public void OnRequest(IRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
2020-05-25 17:00:38 +01:00
string? parameters = null;
if (request.Parameters != null)
{
2020-11-13 13:43:00 +00:00
parameters = string.Join(",",
request.Parameters.Select(kv => kv.Key + "=" + kv.Value)?.ToArray() ?? Array.Empty<string>()
2020-11-13 13:43:00 +00:00
);
}
Console.WriteLine(OnRequestFormat, request.Method, request.Endpoint, parameters, request.Body);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303")]
public void OnResponse(IResponse response)
{
Ensure.ArgumentNotNull(response, nameof(response));
2020-11-13 13:43:00 +00:00
string? body = response.Body?.ToString()?.Replace("\n", "", StringComparison.InvariantCulture);
2020-05-16 17:48:32 +01:00
body = body?.Substring(0, Math.Min(50, body.Length));
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
}
}
}