Skip to content

Instantly share code, notes, and snippets.

@Adolfi
Created Oct 6, 2022
Embed
What would you like to do?
public class LogHttpRequestDelegatingHandler : DelegatingHandler
{
private readonly ILogger<LogHttpRequestDelegatingHandler> logger;
public LogHttpRequestDelegatingHandler(ILogger<LogHttpRequestDelegatingHandler> logger)
{
this.logger = logger;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var stopWatch = Stopwatch.StartNew();
this.logger.LogInformation("Calling {HttpMethod} {RequestUri} at: {Now}", request.Method, request.RequestUri, DateTime.Now);
var response = await base.SendAsync(request, cancellationToken);
stopWatch.Stop();
this.logger.LogInformation("Response: StatusCode: {StatusCode}. Executed in: {Elapsed}", response.StatusCode, stopWatch.Elapsed);
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment