2

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

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)

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.

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,

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 class has following constructor,

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

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()
  }
}

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

await _repo.AddAsync()

We use below EFCore packages

enter image description here

we have below in host.json file.

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

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 Source code: https://github.com/jeffhollan/functions-csharp-entityframeworkcore

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",
    }
}

Screenshot as per below comment for KrishnenduGhosh-MSFT. enter image description here

enter image description here

Logs from stackify. enter image description here

Vicky
  • 624
  • 2
  • 12
  • 35

1 Answers1

0

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"}
  }
}

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://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

krishg
  • 5,935
  • 2
  • 12
  • 19
  • I tried above but @KrishnenduGhosh-MSFT but unable to see any logs for Azure SQL. – Vicky Aug 30 '20 at 21:35
  • yes I removed nuget and even the line of code you referred. Yeah, even I feel I am missing some basic thing which I am unable to catch. Add the screenshot to above question. – Vicky Aug 31 '20 at 04:09
  • Can you add dependency in excludedTypes. Also note excludedTypes (not samplingExcludedTypes) should be inside samplingSettings as per documentation. Update logging config in my answer above. – krishg Aug 31 '20 at 11:28
  • I tried above @KrishnenduGhosh-MSFT. Unfortunately, the dependency map do not have azure sql. Though the logs relevant to opening/closing connections, commands are showing up on my Visual studio debug console, they are somehow not flowing to App insights I guess. – Vicky Aug 31 '20 at 14:23
  • Does your other log statements in the application (custom logs) via ILogger showing up in App Insights? If you do not have any currently, just add something dummy like `log.LogInformation("Hello world");` somewhere in your main run method of the Function to see if that appears in AI. – krishg Aug 31 '20 at 14:24
  • pls take a look at above screenshot. Also, I looked at the live metrics. It is not showing up I guess. Let me know if there is some specific place I have to look at. I am able to see the log we just added in stackify. Though I am able to traces like function started, executed, executing, or error exceptions and all in app insights. – Vicky Aug 31 '20 at 15:41
  • sorry could not follow you fully regarding "hello world" log statement. It's showing up or not showing up in App Insights (leave the stackify stuffs for now to avoid confusion)? From your screenshot it looks like it's NOT in app insights (though casing in your search and the actual string your logged are not same, correct and search again). Also check in app insights query pane ("Log" button will open that) with query `traces where message contains "Hello world"` – krishg Aug 31 '20 at 15:48
  • sorry for the confusion. I executed the following (traces | where message contains "Hello world") and there is no data as such. Message from app insights: No results found from the last 24 hours – Vicky Aug 31 '20 at 16:15
  • Great, now I can narrow down the focus :). It's not only sql dependency, but no log is coming from application scope. – krishg Aug 31 '20 at 16:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220749/discussion-between-krishnendughosh-msft-and-vicky). – krishg Aug 31 '20 at 16:20
  • for some reason, I am unable to open chat discussion. I have degraded the version of EF core to 3.1.3 as per below recommendation and it worked well. SQL is appearing as dependency in app insights. But, not sure if that is correct solution because degrading the version will not have latest EF Core changes. https://github.com/Azure/Azure-Functions/issues/1613#issuecomment-687062296 – Vicky Sep 11 '20 at 03:36
  • Is there a way that I can use latest version of EF Core and still ensure everything is working fine? – Vicky Sep 11 '20 at 03:38
  • EF core version 3.1.8 is out, can you try that. Also update Microsoft.NET.Sdk.Functions to 3.0.9. Though it's less likely got fixed. – krishg Sep 11 '20 at 08:10