简体   繁体   English

如何登录Entity Framework Core UseSqlServer重试

[英]How to log in Entity Framework Core UseSqlServer Retries

I have configured Retries Policies in DbContextOptionsBuilder.UseSqlServer using Entity Framework Core 2.1. 我已经使用实体框架核心2.1在DbContextOptionsBuilder.UseSqlServer配置了重试策略。

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. 我在这里检查了一个相关的问题: azure sql数据库,但是该问题与实体框架6和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? 例如,在重试过程中,它将打印一条日志,显示等待#5中的第1个重试?

Thank you. 谢谢。

You can add logging to your DbContext in OnConfiguring method. 您可以在OnConfiguring方法中将日志记录添加到DbContext中。 UseLoggerFactory method takes LoggerFactory, this is asp.net core loggerFactory. UseLoggerFactory方法使用LoggerFactory,这是asp.net的核心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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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