【发布时间】:2017-03-22 12:46:52
【问题描述】:
我考虑使用Polly 创建策略来记录异常并重新抛出。 我没有找到允许它开箱即用的现有方法,但我看到的一些选项是
后备
// Specify a substitute value or func, calling an action (e.g. for logging)
// if the fallback is invoked.
Policy.Handle<Whatever>()
.Fallback<UserAvatar>(UserAvatar.Blank,
onFallback: (exception, context) =>
{
_logger.Log(exception, context);
throw exception;
});
问题:Fallback 可以抛出异常吗?
超时
Policy.Timeout(1, T30meoutStrategy.Pessimistic,
(context, timespan, task) =>
{
// ContinueWith important!: the abandoned task may very well still be executing,
// when the caller times out on waiting for it!
task.ContinueWith(t =>
{
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 并自己编写 简单的 try/catch 和 throw。
这些方法中哪一种更好? 你有什么建议?
【问题讨论】: