记录异常并重新抛出的Polly策略 [英] Polly policy to log exception and rethrow
问题描述
我考虑使用 Polly 来创建政策记录异常并重新抛出。
我没有找到一种允许使用的现成方法,但是我看到的一些选项是
I consider to use Polly to create policy to log exception and rethrow. I didn't find an existing method that allow it out of the box , but some options that I see are
Fallback
// 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;
});
问题:是否可以从Fallback引发异常?
Question: is it ok to throw exception from Fallback?
超时
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次重试?
Question: is 0 retries supported?
或亲吻并写简单的try / catch
Or KISS and write simple try/catch with throw by myself.
以下哪种方法更好?
您的建议是什么?
Which of these methods is better? What are your recommendation?
推荐答案
如果您还没有Polly,可以尝试一下/抓住最简单的。
If you do not already have Polly in the mix, try/catch would seem simplest.
如果您已经有了Polly,请 FallbackPolicy
可以按照您建议的方式安全地重新利用。 onFallback
委托和后备操作或值不受政策的 .Handle<>()
子句约束,因此您可以安全地从 onFallback
委托中抛出异常。
If you already have Polly in the mix, FallbackPolicy
can safely be re-purposed in the way you suggest. The onFallback
delegate and fallback action or value are not governed by the .Handle<>()
clauses of the Policy, so you can safely rethrow an exception from within the onFallback
delegate.
Policy<UserAvatar>.Handle<Whatever>()
.Fallback<UserAvatar>(UserAvatar.Blank, onFallback: (exception, context) =>
{ _logger.Log(exception, context);
throw exception;
});
使用 TimeoutPolicy
仅捕获由呼叫者由于超时而较早离开的代表,仅在 TimeoutMode.Pessimistic
中;并非所有例外。
The approach your question outlines with TimeoutPolicy
would only capture exceptions thrown by delegates the caller had earlier walked away from due to timeout, and only in TimeoutMode.Pessimistic
; not all exceptions.
您的问题使用概述的方法.Retry(0,... )
无效。如果未指定重试,则不会调用 onRetry
委托。
The approach your question outlines with .Retry(0, ...)
would not work. If no retries are specified, the onRetry
delegate would not be invoked.
为避免重新使用 FallbackPolicy
的麻烦,您还可以在Polly的结构内编写自己的 LogThenRethrowPolicy
。 此提交(其中添加了简单的 NoOpPolicy
)说明了添加新策略所需的最低要求。您可以添加类似于 NoOpPolicy
的实现,但只需 try {} catch {/ * log;重新抛出* /}
To avoid the untidiness of repurposing FallbackPolicy
, you could also code your own LogThenRethrowPolicy
, within Polly's structures. This commit (which added the simple NoOpPolicy
) exemplifies the minimum necessary to add a new policy. You could add an implementation similar to NoOpPolicy
but just try { } catch { /* log; rethrow */ }
编辑2019年1月:Polly。 Contrib现在还包含一个 Polly.Contrib.LoggingPolicy 可以提供帮助。
EDIT January 2019: Polly.Contrib now also contains a Polly.Contrib.LoggingPolicy which can help with this.
这篇关于记录异常并重新抛出的Polly策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!