Polly policy to log exception and rethrow
Asked Answered
C

3

14

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?

Cholla answered 22/3, 2017 at 12:46 Comment(0)
S
21

If you do not already have Polly in the mix, try/catch would seem simplest.

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;
    });

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.


The approach your question outlines with .Retry(0, ...) would not work. If no retries are specified, the onRetry delegate would not be invoked.


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 */ }


EDIT January 2019: Polly.Contrib now also contains a Polly.Contrib.LoggingPolicy which can help with this.

Spiritualism answered 25/3, 2017 at 12:17 Comment(1)
Do you know where the NuGet package is for the LoggingPolicy? I don't see a link on the GitHub, and I tried searching NuGet for Polly.Contrib and Polly.Contrib.LoggingPolicy. Neither package seems to exist, although I found some sub-packages for Polly.Contrib that didn't seem related.Goldston
B
6

https://github.com/App-vNext/Polly-Samples/blob/master/PollyDemos/Async/AsyncDemo02_WaitAndRetryNTimes.cs shows that you can use the onRetry: option, at least for WaitAndRetryAsync. I haven't looked at the others yet.

HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3,
    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))  // exponential back-off: 2, 4, 8 etc
                    + TimeSpan.FromMilliseconds(Jitterer.Next(0, 1000)), // plus some jitter: up to 1 second
    onRetry: (response, calculatedWaitDuration) =>
    {
        logger.LogError($"Failed attempt. Waited for {calculatedWaitDuration}. Retrying. {response.Exception.Message} - {response.Exception.StackTrace}");
    }
);
Baronet answered 2/11, 2018 at 13:2 Comment(1)
I didn’t want to retry, just log and re-throw.Cholla
A
1

Here my solution with generic method

public async Task<T> PollyRetry<T>(
        Func<Task<T>> action)
    {

        bool hasFallback = false;
        Exception ex = null;

        var fallbackPolicy = Policy<T>.Handle<Exception>().FallbackAsync(
            default(T), d =>
            {
                //log final exception

                ex = d.Exception;

                hasFallback = true;
                return Task.FromResult(new { });

            });

        var retryPolicy = Policy
            .Handle<Exception>()
            .WaitAndRetryAsync(3,
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                (res, timeSpan, context) =>
                {
                    //log exception
                });

        var policyResult = await fallbackPolicy.WrapAsync(retryPolicy).ExecuteAndCaptureAsync(action);

        if (hasFallback && ex != null)
            throw ex;

        return policyResult.Result;
    }

//call service with retry logic
        TestResponse response = await _pollyRetryService.PollyRetry(async () =>
        {
            return await _testService.Test(input);

        });
Ambsace answered 7/10, 2021 at 9:16 Comment(1)
I would suggest to use ExceptionDispatchInfo instead of re-throwing the exception.Cabe

© 2022 - 2024 — McMap. All rights reserved.