Log when entity framework 6 SqlAzureExecutionStrategy retries

不羁的心 提交于 2019-12-10 22:17:57

问题


How can I log when the SqlAzureExecutionStrategy retries an Entity Framework operation? When using the transient fault handling logic with regular SqlConnection calls, a Retrying event is exposed. Is there something similar when using the SqlAzureExecutionStrategy with Entity Framework 6? If not, what are some other options?


回答1:


There is a very good article on this topic explaining all the steps for logging Azure SQL database with ASP.NET MVC and EntityFramework:. Hope this helps. Here is the link to it: asp.net/mvc/overview/getting-started/… – ACS yesterday

As stated in the comment by ACS, this guide discusses connection resiliency and command interception for Entity Framework.This document discusses how to built the retry logic in EF6 and how to create a logging interface and class to log the events.

In addition to the previous link, there is a topic which discusses connection resiliency/retry logic in EF6 and its limitations.

Hope this helps!




回答2:


You can create a custom ExecutionStrategy which inherits from the SqlAzureExecutionStrategy, and override the ShouldRetryOn method:

public class LoggedSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
{
  protected override bool ShouldRetryOn(Exception exception)
  {
    var shouldRetry = base.ShouldRetryOn(exception);
    if (shouldRetry)
    {
      // log exception   
    }

    return shouldRetry;
  }
}


来源:https://stackoverflow.com/questions/28949069/log-when-entity-framework-6-sqlazureexecutionstrategy-retries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!