2

我们有基于 .net Core 3.1 的 Azure 函数。我们使用最新版本的 EntityFrameworkCore。

它连接到 Azure SQL 以存储/检索/查询数据。我们有时可以在应用洞察的实时流中看到 Azure SQL 的日志,例如打开连接、关闭连接(有时可能是因为启用了采样)

但是,我们在应用程序洞察力的应用程序图中看不到 Azure SQL 依赖项。甚至,查看跟踪表,我看不到任何与 Azure SQL 相关的内容。

是否需要启用 Azure SQL 以显示为依赖项?我在几篇 m​​sdn 文章中读到,当您使用 Microsoft.Data.SqlClient 包时会自动检测到 SQL(我看到 EF 核心已经在内部安装了该包)。

如果上述问题得到回答和解决,还有一个后续问题 - 有没有办法,我可以检查连接是否被释放/关闭,或者连接何时打开/关闭对于 App 洞察中的给定函数调用?

根据以下评论,添加更多信息,

我们在启动文件中使用以下语句将 DbContext 添加到服务中。

builder.Services.AddDbContextPool<OurDbContext>(options =>
{
    options.UseSqlServer("connectionstring"), builder =>
    {
       builder.EnableRetryOnFailure(3, TimeSpan.FromSeconds(2), null);
    });
});

OurDbContext 类具有以下构造函数,

public OurDbContext(DbContextOptions<OurDbContext> options)
    : base(options)
{
}

然后我们在不同的存储库中注入 OurDbContext 类,这些存储库使用此上下文与 SQL 对话。类似于下面:

public class Repo : IRepo
{
  public Repo(OurDbContext ourDbContext)
  {

  }
  
  public async Task AddAsync(Entity entity)
  {
    ourDbContext.AddAsync(entity);
    ourDbContext.SaveChangesAsync()
  }
}

我们将这些 repos 注入到 Function 类中并调用上述方法,例如

await _repo.AddAsync()

我们使用以下 EFCore 包

在此处输入图像描述

我们在 host.json 文件中有以下内容。

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingExcludedTypes": "Request",
            "samplingSettings": {
                "isEnabled": true
            }
        }
    }
}

注意:我尝试下面的链接只是为了检查 sql 依赖项是否显示在 App 洞察中,尽管它不使用我正在使用的 EFCore/最新版本的 Azure 函数的配置。我唯一添加的是本地设置中的 APPINSIGHTS_INSTRUMENTATIONKEY 。

https://dev.to/azure/using-entity-framework-with-azure-functions-50aa GitHub源代码: https ://github.com/jeffhollan/functions-csharp-entityframeworkcore

通过以上,我能够在我的应用洞察力中看到 SQL 依赖关系。但是,当我在上面修改为我当前项目使用的 Azure 函数、.net core、EFCore 版本时,SQL 依赖项不再出现在 App 洞察中。但是,添加低于日志记录级别会在控制台中显示调试日志。

"Logging": {
    "LogLevel": {
      "Default": "Debug",
    }
}

根据以下 KrishnenduGhosh-MSFT 评论的屏幕截图。 在此处输入图像描述

在此处输入图像描述

来自 stackify 的日志。 在此处输入图像描述

4

1 回答 1

0

如下更新 host.json 中的日志记录部分以允许信息级别日志(注意我在您上面发布的现有配置中添加了 logLevel)。默认情况下,如果您未指定,则为警告。正如此处的注释中所述,依赖项使用信息级别记录。另请注意,根据文档, excludedTypes(不是 samplingExcludedTypes)应该在samplingSettings内。

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Dependency;Request"
            }
        },
    "logLevel": {"default": "Information"}
  }
}

同样对于 Azure 功能,您不应在 Startup 中添加microsoft.applicationinsights.aspnetcorenuget 和。builder.Services.AddApplicationInsightsTelemetry();这适用于 asp.net 核心应用程序。函数不应该有那个https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

于 2020-08-30T10:00:45.890 回答