Using Npgsql EnableRetryOnFailure - is there any visibility / logging when a retry occurs?

1.8k views Asked by At

I'm using EnableRetryOnFailure in a .Net core EF application and have added logic as per https://www.npgsql.org/efcore/misc/other.html. I'd like to be able to tell when/if these events happen as a confirmation that it's working, and not just the absence of any transient connection errors.

Thanks in advance for any pointers. Reuven

1

There are 1 answers

0
Pete Kayson On

Logging can be enabled through an override in your context class.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.LogTo(
        filter: (eventId, level) => eventId.Id == CoreEventId.ExecutionStrategyRetrying,
        logger: (eventData) =>
        {
            var retryEventData = eventData as ExecutionStrategyEventData;
            var exceptions = retryEventData.ExceptionsEncountered;
            Console.WriteLine($"Retry #{exceptions.Count} with delay {retryEventData.Delay} due to error: {exceptions.Last().Message}");
        });
}

Source: https://makolyte.com/how-to-do-retries-in-ef-core/