Skip to content

Instantly share code, notes, and snippets.

Created Oct 6, 2022
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);
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