App-vNext/Polly

Pass arbitrary data to Policy execution

Closed this issue · 2 comments

I have a generic policy, like this:

        private static readonly Policy _basicRetryPolicy = Policy
                                        .Handle<Exception>()
                                        .WaitAndRetry(3,
                                        retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt + 3)),  //exponetial backoff, 2^4, 2^5 and 2^6 seconds between retry (16sec, 32sec and 64sec)
                                        (exception, timespan) =>
                                        {
                                            Logger.Info("Retry operation - wait :" + timespan.TotalSeconds.ToString());
                                        });

Now you can see I am writing to a log when the retry operation happens. However, I am using this same policy for many different Execute() functions. I would like to perhaps be able to pass some data, maybe just a string that says what call this is - so when the log is written it can accurately record which operation is retrying.

Is there a way to do this - if not, do you think it is a valuable addition to your library (perhaps as an optional parameter to the Execute() call - and then that parameter passed into the lambdas like timespan/retryattempt/exception is?

I think this is a really good idea and I will look into implementing it.

In the meantime, you can probably get information about the call that raised the exception from either the exception itself or the call stack. See How to get the name of the method that caused the exception for more information.

Great, thanks...