简体   繁体   中英

How to log in Entity Framework Core UseSqlServer Retries

I have configured Retries Policies in DbContextOptionsBuilder.UseSqlServer using Entity Framework Core 2.1.

Here is the code sample:

DbContextOptionsBuilder<T> builder = new DbContextOptionsBuilder<T>();
builder.UseSqlServer(ConnectionString, sqloptions => {
                sqloptions.EnableRetryOnFailure(
                    maxRetryCount: 5,
                    maxRetryDelay: TimeSpan.FromSeconds(30),
                    errorNumbersToAdd: new List<int>(){});
            });

The code above works but I do not know how to add logging during the retries process.

I have checked a related question here: azure sql database but that question is related to entity framework 6 and sql azure.

Could anyone suggest how to add logging in the retries policies? For example, during the retries process, it will print a log saying waiting for retries #1 out of #5 for example?

Thank you.

You can add logging to your DbContext in OnConfiguring method. UseLoggerFactory method takes LoggerFactory, this is asp.net core loggerFactory.

This should write retries to your log.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);
    optionsBuilder.UseLoggerFactory(LoggerFactory);
}

You can find detailed information here.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM