Skip to content

Commit b532fb4

Browse files
authored
Merge pull request #2 from johnkors/log-failed-responses
Log failed responses
2 parents f205104 + 76ab0b8 commit b532fb4

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

samples/With/Worker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2424
{
2525
_logger.LogInformation("Timeout!");
2626
}
27+
catch(Exception e)
28+
{
29+
_logger.LogError(e.Message);
30+
}
2731

2832
await Task.Delay(10000, stoppingToken);
2933
}

samples/Without/Worker.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2020
{
2121
await _client.GetAsync("https://httpstat.us/404?sleep=3000", stoppingToken);
2222
}
23-
catch (TaskCanceledException e)
23+
catch (TaskCanceledException)
2424
{
2525
_logger.LogInformation("Timeout!");
2626
}
27+
catch(Exception e)
28+
{
29+
_logger.LogError(e.Message);
30+
}
2731

2832
await Task.Delay(10000, stoppingToken);
2933
}

source/MinimalHttpLogger/MinimalHttpLogger.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,19 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5858
{
5959
throw new ArgumentNullException(nameof(request));
6060
}
61-
var requestUri = request.RequestUri.ToString(); //SendAsync modifies req uri in case of redirects (?!), so making a local copy
61+
var requestUri = request.RequestUri?.ToString(); //SendAsync modifies req uri in case of redirects (?!), so making a local copy
6262
var stopwatch = ValueStopwatch.StartNew();
63-
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
64-
_logger.LogInformation("{Method} {Uri} - {StatusCode} {StatusCodeLiteral} in {Time}ms", request.Method, requestUri, $"{(int)response.StatusCode}", $"{response.StatusCode}", stopwatch.GetElapsedTime().TotalMilliseconds);
65-
66-
return response;
63+
try
64+
{
65+
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
66+
_logger.LogInformation("{Method} {Uri} - {StatusCode} {StatusCodeLiteral} in {Time}ms", request.Method, requestUri, $"{(int)response.StatusCode}", $"{response.StatusCode}", stopwatch.GetElapsedTime().TotalMilliseconds);
67+
return response;
68+
}
69+
catch(Exception)
70+
{
71+
_logger.LogInformation("{Method} {Uri} failed to respond in {Time}ms", request.Method, requestUri, stopwatch.GetElapsedTime().TotalMilliseconds);
72+
throw;
73+
}
6774
}
6875

6976
internal struct ValueStopwatch

0 commit comments

Comments
 (0)