Azure SQL依赖关系(基于EF Core 3.1.7)未出现在App Insights的应用程序映射中 [英] Azure SQL dependency (based on EF Core 3.1.7) is not appearing in Application Map of App Insights

本文介绍了Azure SQL依赖关系(基于EF Core 3.1.7)未出现在App Insights的应用程序映射中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们具有基于.net Core 3.1的Azure功能.我们使用最新版本的EntityFrameworkCore.

We have Azure functions based on .net Core 3.1. We use latest version of EntityFrameworkCore.

它连接到Azure SQL以存储/检索/查询数据.有时我们可以在Live App of Stream见解中看到Azure SQL的日志,例如打开连接",关闭连接"(有时可能是由于启用了采样)

It connects to Azure SQL to store/retrieve/query data. We are able to see logs for Azure SQL such as Opening connection, closing connection in Live Stream of App insights sometimes (sometimes may be because of Sampling being enabled)

但是,我们在应用程序见解"的应用程序映射"中看不到Azure SQL依赖性.甚至,在跟踪表中,我都没有看到与Azure SQL相关的任何信息.

But, we don't see Azure SQL dependency in Application map of App insights. Even, looking at the traces table, I don't see anything related to Azure SQL.

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

Is there something we need to enable for Azure SQL to show as dependency? I read in few msdn articles, that it is automatically detected for SQL when you use Microsoft.Data.SqlClient package (and I see EF core has that package installed already internally).

还有一个后续问题,如果以上问题得到解决,是否可以解决?我可以检查是否断开/关闭了连接,或者何时为应用程序见解中的给定功能调用打开/关闭了连接?

Also a follow up question if above is answered and resolved - is there a way, I can check if connection is disposed/closed or when was the connection opened/closed for given function invocation in App insights?

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

As per below comment, adding more info,

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

We add DbContext to the services using following statement in the startup file.

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

OurDbContext类具有以下构造函数,

OurDbContext class has following constructor,

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

然后我们将OurDbContext类注入到不同的存储库中,该存储库使用此上下文与SQL进行通信.类似于以下内容:

And then we inject OurDbContext class in different repositories which uses this context to talk to SQL. Similar to below:

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

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

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

We inject these repos in Function classes and call above methods such as

await _repo.AddAsync()

我们在下面使用EFCore软件包

We use below EFCore packages

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

we have below in host.json file.

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

注意: 我尝试在下面的链接中进行检查,只是检查sql依赖项是否出现在App见解中,尽管它没有使用我正在使用的EFCore/最新版本的Azure函数.我唯一添加的是本地设置中的APPINSIGHTS_INSTRUMENTATIONKEY.

Note: I tried below link just to check if sql dependency is showing up in App insights, though it does not use the configuration of EFCore/latest version of Azure functions which I am using. Only thing I have added is APPINSIGHTS_INSTRUMENTATIONKEY in my local settings.

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

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

通过上面的内容,我能够在我的应用洞见中看到SQL依赖关系. 但是,当我在上面修改Azure函数的版本(.net核心,EFCore)时,我正在当前的项目中使用,SQL依赖项不再出现在App见解中.不过,添加到日志级别以下会在控制台中显示调试日志.

With above, I was able to see SQL dependency in my app insights. But, when I modified above to the version of Azure functions, .net core, EFCore, I am using for my current project, SQL dependencies stopped appearing in App insights. Though, adding below logging level shows debug logs in Console.

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

按照下面的注释为KrishnenduGhosh-MSFT截屏.

Screenshot as per below comment for KrishnenduGhosh-MSFT.

stackify的日志.

Logs from stackify.

推荐答案

如下所示更新host.json中的日志记录部分,以允许信息级别日志(请注意,我在上面发布的现有配置中添加了logLevel).默认情况下,如果未指定,则为警告.如注释此处,相关性以信息级别记录.还要注意,按照

Update your logging section in host.json as below to allow Information level log (note I added logLevel in the existing config you posted above). By default it's Warning if you do not specify. As mentioned in the Note here, dependencies are logged with Information level. Also note excludedTypes (not samplingExcludedTypes) should be inside samplingSettings as per documentation.

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

对于Azure功能,也不应在启动中添加microsoft.applicationinsights.aspnetcore nuget和builder.Services.AddApplicationInsightsTelemetry();.这是针对asp.net核心应用程序的.函数不应具有 https ://docs.microsoft.com/zh-CN/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

Also for Azure function, you should not add microsoft.applicationinsights.aspnetcore nuget and builder.Services.AddApplicationInsightsTelemetry(); in Startup. That's for asp.net core application. Function should not have that https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

这篇关于Azure SQL依赖关系(基于EF Core 3.1.7)未出现在App Insights的应用程序映射中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆