我認爲使用Polly來創建策略來記錄異常並重新拋出。 我沒有找到一個現有的方法,允許它開箱即用,但有些選項,我看到的是Polly策略來記錄異常並重新拋出
後備
// Specify a substitute value or func, calling an action (eg for logging) if the fallback is invoked.
Policy.Handle<Whatever>()
.Fallback<UserAvatar>(UserAvatar.Blank, onFallback: (exception, context) =>
{ _logger.Log(exception, context);
throw exception;
});
問:是否確定從後備拋出異常?
超時
Policy.Timeout(1, T30meoutStrategy.Pessimistic, (context, timespan, task) =>
{ task.ContinueWith(t =>
{ // ContinueWith important!: the abandoned task may very well still be executing, when the caller times out on waiting for it!
if (t.IsFaulted )
{
logger.Error(context,t.Exception);
throw exception;
} );
或者重試
Policy.Handle<DivideByZeroException>().Retry(0, (exception, retryCount) =>
{ logger.Error(context,exception);
throw exception;
} );
問題:支持0重試?
或KISS並寫簡單的嘗試/捕獲與自己扔。
哪種方法比較好? 您的建議是?
如果我能賞金你的問題我會的。這個問題非常有幫助! –