Skip to content

Commit

Permalink
fix: rest log (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcfialho authored Jun 5, 2023
1 parent 969fa3e commit 4e9089a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion content/WebApi/WebApi.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"WebServices": {
"ICepService": {
"Url": "https://brasilapi.com.br/api",
"InitialDelay": 10,
"RetryDelay": 10,
"RetryCount": 3
}
},
Expand Down
8 changes: 4 additions & 4 deletions content/WebApi/WebApi.Infrastructure/Configurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection servi
{
var typeName = typeof(T).Name;

RequiredConfiguration<T>(configuration, $"WebServices:{typeName}:InitialDelay");
RequiredConfiguration<T>(configuration, $"WebServices:{typeName}:RetryDelay");
RequiredConfiguration<T>(configuration, $"WebServices:{typeName}:RetryCount");
RequiredConfiguration<T>(configuration, $"WebServices:{typeName}:Url");

int.TryParse(configuration[$"WebServices:{typeName}:InitialDelay"], out int initialDelay);
int.TryParse(configuration[$"WebServices:{typeName}:RetryDelay"], out int initialDelay);
int.TryParse(configuration[$"WebServices:{typeName}:RetryCount"], out int retryCount);

var retryDelay = Backoff.ExponentialBackoff(TimeSpan.FromSeconds(initialDelay), retryCount);
Expand All @@ -131,7 +131,7 @@ public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection servi
.AddPolicyHandler((services, request) => HttpPolicyExtensions.HandleTransientHttpError().OrResult(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(retryDelay, onRetry: (response, timespan, retryAttempt, context) =>
{
var logger = services.GetRequiredService<ILogger<ICepService>>();
var logger = services.GetRequiredService<ILogger<T>>();
var content = request?.Content?.ReadAsStringAsync()?.Result;
logger.LogWarning($"Delaying request {request?.Method?.Method} {request?.RequestUri?.OriginalString} for {timespan.TotalSeconds} seconds then making retry {retryAttempt} - statuscode {response?.Result?.StatusCode} - content {content}");
}))
Expand All @@ -140,7 +140,7 @@ public static IHttpClientBuilder AddRefitClient<T>(this IServiceCollection servi
(response, context, cancellationToken) => Task.FromResult(response.Result),
(context, cancellationToken) =>
{
var logger = services.GetRequiredService<ILogger<ICepService>>();
var logger = services.GetRequiredService<ILogger<T>>();
logger.LogError(context.Exception, $"Error while processing request {request?.Method?.Method} {request?.RequestUri?.OriginalString}");
if (context.Exception != null) throw context.Exception;
return Task.CompletedTask;
Expand Down

0 comments on commit 4e9089a

Please sign in to comment.