One simple approach is to use a do
/while
nested loop inside the main loop, and count retries there, like this:
foreach (var item in allReportingItems) {
var retries = 0;
var reportIsGenerated = false;
do {
reportIsGenerated = TryGeneratingReport(item);
retries++;
// The loop will end when the report gets generated
// or when the retries count would be exhausted
} while (!reportIsGenerated && retries < 3);
if (!reportIsGenerated) {
Concole.Error.WriteLine(
"Unable to generate report for {0} after 3 retries", item
);
}
}