Skip to content

Instantly share code, notes, and snippets.

@Adolfi
Created October 6, 2022 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adolfi/8bd576f8e4fd688c8f0c2cd519ab45b5 to your computer and use it in GitHub Desktop.
Save Adolfi/8bd576f8e4fd688c8f0c2cd519ab45b5 to your computer and use it in GitHub Desktop.
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