forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleConsoleHTTPLogger.cs
More file actions
37 lines (32 loc) · 1.13 KB
/
SimpleConsoleHTTPLogger.cs
File metadata and controls
37 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Linq;
namespace SpotifyAPI.Web.Http
{
public class SimpleConsoleHTTPLogger : IHTTPLogger
{
private const string OnRequestFormat = "\n{0} {1} [{2}] {3}";
public void OnRequest(IRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
string? parameters = null;
if (request.Parameters != null)
{
parameters = string.Join(",",
request.Parameters.Select(kv => kv.Key + "=" + kv.Value)?.ToArray() ?? Array.Empty<string>()
);
}
Console.WriteLine(OnRequestFormat, request.Method, request.Endpoint, parameters, request.Body);
}
public void OnResponse(IResponse response)
{
Ensure.ArgumentNotNull(response, nameof(response));
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
string? body = response.Body?.ToString()?.Replace("\n", "", StringComparison.InvariantCulture);
#else
string? body = response.Body?.ToString()?.Replace("\n", "");
#endif
body = body?.Substring(0, Math.Min(50, body.Length));
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
}
}
}