Files

34 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jugenddienst_Stunden.Infrastructure;
internal sealed class RequestLoggingHandler : DelegatingHandler {
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
// Log outgoing request URI + headers
Debug.WriteLine($"[Http] Request: {request.Method} {request.RequestUri}");
foreach (var h in request.Headers) {
Debug.WriteLine($"[Http] RequestHeader: {h.Key} = {string.Join(", ", h.Value)}");
}
if (request.Content is not null) {
foreach (var h in request.Content.Headers) {
Debug.WriteLine($"[Http] ContentHeader: {h.Key} = {string.Join(", ", h.Value)}");
}
}
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
// Log response status + Location (bei Redirects) + final request URI used by handler
Debug.WriteLine($"[Http] Response: {(int)response.StatusCode} {response.ReasonPhrase}");
if (response.Headers.Location is not null)
Debug.WriteLine($"[Http] Response Location: {response.Headers.Location}");
if (response.RequestMessage?.RequestUri is not null)
Debug.WriteLine($"[Http] Final RequestUri: {response.RequestMessage.RequestUri}");
return response;
}
}