Search code examples
azureentity-framework-6azure-application-insights.net-4.7.2

How to get the full SQL query in Application Insights


I just watched the video https://www.youtube.com/watch?v=TV1u6UoBRwk and saw the section on End to End transactions. I've used that to track down a SQL query that is taking 1.8 mins.

The query is truncated (probably because EF is generating a super long query).

Is there any way to get the full query from Application Insights? enter image description here


Solution

    • By default, SQL Collection is set to off. We need to enable it in the Azure Portal.
    • To get the full SQL Query, we need to enable the SQL Commands in our deployed App Service => Application Insights.

    enter image description here

    • Add the below line of code in Program.cs file, if it is .NET Core Applications.
    builder.Services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) => { module.EnableSqlCommandTextInstrumentation = true; });
    
    • If it is .NET Framework Application, add the below line in TelemetryModules section in ApplicationInsights.config file (which is generated when we add Application Insights in Visual Studio.
    <Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">, 
    <EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
    </Add>
    

    References taken from MSDoc and GitHub.