Logging in OnRetry #2070
-
Hello, Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Well, it depends. Are you the one who is calling the If you can provide an example how do you plan to define and use the retry strategy then I can provide some suggestions. |
Beta Was this translation helpful? Give feedback.
-
I'm defining the pipeline like this:
And starting the execution like this:
By the way, I'm not really sure whether it is better to use the |
Beta Was this translation helpful? Give feedback.
-
In this particular scenario, you can use the following two options:
Option 1Your current code declares the class XYZ
{
private readonly ILogger<XYZ> logger;
private readonly ResiliencePipeline resiliencePipeline;
public XYZ(ILogger<XYZ> logger)
{
this.logger = logger;
this.resiliencePipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions
{
...
OnRetry = args =>
{
...
this.logger....
return default;
},
})
.Build();
}
...
} Option 2You can pass the logger to the strategy execution in the exact same way as you did with the resilienceContext.Properties.Set(ResilienceContextKeys.Logger, logger);
...
if(args.Context.Properties.TryGetValue(ResilienceContextKeys.Logger, out var logger)) Side notes
services.AddResiliencePipeline("my-key", (builder, context) =>
{
var loggerFactory = context.ServiceProvider.GetRequiredService<ILoggerFactory>();
builder.AddRetry(...);
}); |
Beta Was this translation helpful? Give feedback.
-
Thanks, @peter-csala, I'll probably try Option 1 or just use the DI. |
Beta Was this translation helpful? Give feedback.
In this particular scenario, you can use the following two options:
ILogger
inside theOnRetry
delegateILogger
to the strategy executionOption 1
Your current code declares the
resiliencePipeline
asreadonly
and defines its value at the same time. If you could define its value inside a constructor then you can refer theILogger
without the need to use closure: