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 (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;
});
Question: is it ok to throw exception from Fallback?
Timeout
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;
}
});
}
Or Retry
Policy.Handle<DivideByZeroException>().Retry(0,
(exception, retryCount) =>
{
logger.Error(context,exception);
throw exception;
});
Question: is 0 retries supported?
Or KISS and write simple try/catch with throw by myself.
Which of these methods is better? What are your recommendation?