使用 EF Core 在 Azure Functions 的 Application Insights 中启用 Sql 依赖项

Enable Sql Dependency in Application Insights on Azure Functions with EF Core

我有一个使用 Microsoft.EntityFrameworkCore 3.1.5 的 Azure Function v3 应用程序。我无法启用 SQL 依赖跟踪。已经苦苦挣扎 1 天了。

为了隔离问题,我创建了一个没有 EF 的独立 AzureFunction。我使用的是 Microsoft.Data.SqlClient 1.0.19269.1 而不是 EF,它也被 EF 使用。 函数如下:

 public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var ids = "";
            using (var connection = new SqlConnection("connectionstring"))
            {
                var command = connection.CreateCommand();
                command.CommandText = "SELECT TOP 10 Id FROM table";
                await connection.OpenAsync();
                using (var reader = await command.ExecuteReaderAsync())
                {
                    while (reader.Read())
                    {
                        ids += reader.GetInt32(0).ToString() + ",";
                    }

                    ids = ids.Substring(0, ids.Length - 1);
                }
            }
            return new OkObjectResult("Ids:" + ids);
        }
    }

应用参考资料

    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="1.0.19269.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.5" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />

将应用程序部署到 Azure(没有 EF)后,SQL 依赖项按预期进行跟踪。

在向项目添加引用 Microsoft.EntityFrameworkCore 且未更改任何其他内容后,跟踪停止工作。删除参考跟踪后再次工作。 更新对最新版本的引用也无济于事。

你知道为什么会这样吗?有什么解决办法的建议吗?

他们似乎在 Microsoft.EntityFrameworkCore 3.1.4 中引入了导致此行为的问题。降级到 3.1.3 即可解决。

更新: 在我打开 github 上的错误后,they pointed me towards an issue 与 Microsoft.Data.SqlClient 和与此相关的 EventSource。