In a .NET 8 project I'm using Serilog and Polly V8 and with ResiliencePipelineBuilder, and this log shows always, even if there is no retry:
Execution attempt. Source: 'screenshot-stream-with-retries/(null)/Retry', Operation Key: 'null', Result: 'System.IO.MemoryStream', Handled: 'false', Attempt: '0', Execution Time: '730.3186'
Here is my code:
public class ScreenshotStreamWithRetries
{
private readonly ILogger<ScreenshotStreamWithRetries> _logger;
public ScreenshotStreamWithRetries(ILogger<ScreenshotStreamWithRetries> logger)
{
_logger = logger;
}
public static string Name => "screenshot-stream-with-retries";
public ResiliencePipelineBuilder<Stream> Configure(ResiliencePipelineBuilder<Stream> pipelineBuilder)
{
return pipelineBuilder.AddRetry(new RetryStrategyOptions<Stream>
{
MaxRetryAttempts = 3,
Delay = TimeSpan.FromSeconds(2),
BackoffType = DelayBackoffType.Exponential,
ShouldHandle = new PredicateBuilder<Stream>()
.Handle<Exception>(),
OnRetry = retryArguments =>
{
_logger.LogWarning($"Screenshot failed with exception {retryArguments.Outcome.Exception.Message}. " +
$"Waiting {retryArguments.Duration} before next retry. Retry attempt {retryArguments.AttemptNumber}");
return ValueTask.CompletedTask;
}
});
}
}
I found this in Polly V8 source code https://github.com/App-vNext/Polly/blob/main/src/Polly.Extensions/Telemetry/Log.cs
I'd like to suppress a log that's creating unnecessary clutter. Can it be done?
TL;DR: No, you can't.
The Polly's telemetry can be enriched but not filtered.
What you see is that the initial attempt (
Attempt: '0') is being performed. It is always emitted regardless the retry strategy is triggered or not.The actual telemetry emission happens inside the
RetryResilienceStrategyThat util class will only omit the telemetry if the entire telemetry is disabled
UPDATE #1
You might need to filter out these messages on the logger itself.
In case of nlog you can do something like this:
For serilog something like this:
Please bear in mind that
ExecutionAttempttelemetry logs are emitted by the Retry and by the Hedging strategies.UPDATE #2
Please bear in mind that this solution is super fragile. Mainly because it relies on an implementation detail:
This can be changed anytime without further notice!