Delegating Handlers in C#

builder.Services.AddHttpClient<WeatherService>();
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
public class LoggingHandler : DelegatingHandler
{
private readonly ILogger<LoggingHandler> _logger;

public LoggingHandler(ILogger<LoggingHandler> logger)
{
_logger = logger;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
_logger.LogInformation("Right before making http request");

var response = await base.SendAsync(request, cancellationToken);

_logger.LogInformation("Response has been received");

return response;
}
}
builder.Services.AddHttpClient<WeatherService>()
.AddHttpMessageHandler<LoggingHandler>();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store