我们有基于.net Core 3.1的Azure功能。我们使用最新版本的 EntityFrameworkCore。
它连接到 Azure SQL 来存储/检索/查询数据。有时我们可以在应用程序见解的实时流中看到 Azure SQL 的日志,例如打开连接、关闭连接(有时可能是因为启用了采样)
但是,我们在应用程序洞察的应用程序地图中没有看到 Azure SQL 依赖项。甚至,查看跟踪表,我没有看到任何与 Azure SQL 相关的内容。
我们是否需要启用某些功能才能使 Azure SQL 显示为依赖项?我在一些 msdn 文章中读到,当您使用 Microsoft.Data.SqlClient 包时,它会自动检测到 SQL(并且我看到 EF core 已在内部安装了该包)。
如果上述问题得到回答和解决,还有一个后续问题 - 有没有办法,我可以检查连接是否已释放/关闭,或者连接何时打开/关闭以在应用程序洞察中进行给定的函数调用?
根据下面的评论,添加更多信息,
我们在启动文件中使用以下语句将 DbContext 添加到服务中。
builder.Services.AddDbContextPool<OurDbContext>(options =>
{
options.UseSqlServer("connectionstring"), builder =>
{
builder.EnableRetryOnFailure(3, TimeSpan.FromSeconds(2), null);
});
});
我们的DbContext类有以下构造函数,
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()
}
}
我们将这些存储库注入到 Function 类中并调用上面的方法,例如
await _repo.AddAsync()
我们使用下面的 EFCore 包
我们在 host.json 文件中有以下内容。
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}
}
笔记:
我尝试下面的链接只是为了检查 sql 依赖项是否显示在 App Insights 中,尽管它不使用我正在使用的 EFCore/最新版本的 Azure 函数的配置。我唯一添加的是本地设置中的 APPINSIGHTS_INSTRUMENTATIONKEY 。
https://dev.to/azure/using-entity-framework-with-azure-functions-50aa https://dev.to/azure/using-entity-framework-with-azure-functions-50aaGitHub 源代码:https://github.com/jeffhollan/functions-csharp-entityframeworkcore https://github.com/jeffhollan/functions-csharp-entityframeworkcore
通过上面的内容,我能够在我的应用程序见解中看到 SQL 依赖性。
但是,当我将上面的内容修改为当前项目使用的 Azure 函数、.net core、EFCore 版本时,SQL 依赖项不再出现在 App Insights 中。不过,添加以下日志记录级别会在控制台中显示调试日志。
"Logging": {
"LogLevel": {
"Default": "Debug",
}
}
Screenshot as per below comment for KrishnenduGhosh-MSFT.
Logs from stackify.