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
38 lines (32 loc) · 1.24 KB
/
SimpleConsoleHTTPLogger.cs
File metadata and controls
38 lines (32 loc) · 1.24 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
38
using System.Linq;
using System;
namespace SpotifyAPI.Web.Http
{
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));
string? parameters = null;
if (request.Parameters != null)
{
parameters = string.Join(",", request.Parameters?.Select(kv => kv.Key + "=" + kv.Value).ToArray());
}
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));
#if NETSTANDARD2_0
string? body = response.Body?.ToString().Replace("\n", "");
#else
string? body = response.Body?.ToString().Replace("\n", "", StringComparison.InvariantCulture);
#endif
body = body?.Substring(0, Math.Min(50, body?.Length ?? 0));
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
}
}
}