Polly: Can we get the call context in the first attempt? [goal: log every attempt] [possible onFirstTry delegate]

Created on 24 Oct 2017  路  4Comments  路  Source: App-vNext/Polly

Hi guys,

I would like to add automatic logging for every request I make using a Policy. It's pretty easy to get all the information I need via context on retry methods, but what about the first shot?

Thanks a lot,

Andre

depends on implementing new syntax enhancement question

All 4 comments

Hi @andrecarlucci

  • There isn't a delegate such as onFirstTry on the retry policy (if that was perhaps along your lines of thinking?). We could consider adding this; if so, it would be done after the major syntax refactor implied by #281. Because of the proliferation of overloads under the existing/historic syntax, we are avoiding adding further overloads at present (already around 120 configuration overloads for retry), but #281 (planned for v6) will make new overloads more viable. Cross-ref #288 .
  • When metrics are added (currently in progress #326), there will be an PolicyExecutionStart and DelegateExecutionStart-type events, which are intended to (optionally) include a projection of Context data. ie In future, it may be possible to harness the metrics stream for this type of logging.
  • For now: if you have a Context object in hand before executing, could one approach be perhaps to simply to call a logging method with that Context, before calling .Execute(...) (or similar)? EDIT: Of course, you could wrap this into an extension method, to avoid code repetition.

Hope this helps

Hi @reisenberger ,

I'm creating a policy to use in many places, so it was about saving some lines of code centralizing this.
No problem, I can proceed the other way and refactor it when the proper method is available in the future.
Thanks a lot for this great library :)

Closing, as a number of suggestions have been made. On further reflection now time has passed: we are possibly unlikely to add an onFirstTry delegate to complement onRetry (even with a configuration syntax refactor that made extra delegates easier to add), as it feels like a burgeoning API surface and still wouldn't centralize the problem (still leaves two API points) for how to log something before _every_ attempt. The best graceful solution to this from within Polly would probably be to hook into a future DelegateExecutionStart event.

A further option

Another alternative, to centralize such code right now, would be simply to write your own extension method to wrap your desired logging into every execution. Something like the below (obviously richer versions would be possible for richer overloads):

TResult LogThenExecute<TResult>(this IPolicy<TResult> policy, Func<TResult> func)
{
    int attempt = 0;
    policy.Execute(() =>
    {
        logger.Log(/* whatever message involving attempt number */);
        attempt++;
        return func();
    }
}

And: Thanks @andrecarlucci for the great question! It provides useful food for thought for a case that events should support.

Was this page helpful?
0 / 5 - 0 ratings