[英]What is the Correct way of logging before Retry using Polly
我正在嘗試在重試之前記錄一些內容。 在重試發生之前記錄信息的正確語法是什么?
這是一個類似於我的實際代碼的示例代碼:
var policy = Polly.Policy
.Handle<SomeExceptionType>()
.WaitAndRetryAsync(
retryCount: this.maxRetryCount,
sleepDurationProvider: (_, e, context) =>
{
var waitTimeSpan = TimeSpan.FromSeconds(1);
if (e != null && e is SomeExceptionType someException)
{
var retryAfterFromException = someException.Headers?.RetryAfter.Delta;
if (retryAfterFromException > TimeSpan.Zero)
{
waitTimeSpan = retryAfterFromException.GetValueOrDefault();
}
}
return waitTimeSpan;
},
onRetryAsync: (e, timeSpan, retryCount) =>
this.logger.LogInfo($"Request failed with {result.Result.StatusCode}. Waiting {timeSpan} before next retry. Retry attempt {retryCount}"));
這會產生語法錯誤,因為LogInfo
返回 void。 什么是正確的日志記錄方法?
將其更改為
onRetryAsync: (e, timeSpan, retryCount, context) => {
this.logger.LogInfo($"Request failed with {result.Result.StatusCode}. Waiting
{timeSpan} before next retry. Retry attempt {retryCount}");
return Task.CompletedTask;
});`
因為 onRetryAsync 需要一個 Task 返回類型
根據您的sleepDurationProvider
委托,您嘗試使用此重載:
public static AsyncRetryPolicy WaitAndRetryAsync(
this PolicyBuilder policyBuilder,
int retryCount,
Func<int, Exception, Context, TimeSpan> sleepDurationProvider,
Func<Exception, TimeSpan, int, Context, Task> onRetryAsync)
這意味着預期的onRetryAsync
應該如下所示:
(exception, sleepDuration, retry, context) =>
{
this.logger.LogInfo(...);
return Task.CompletedTask;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.