Search code examples
c#azure-functionsazure-sql-databaseazure-application-insights

SQL Dependency Tracking Command Text via Dapper


I am trying to log the command text when calling a stored procedure but for some odd reason its only showing the database name and not the stored procedure name. Should mention this is an Azure Function V3

What am I doing wrong / missing?

This is what is output in AI:

tcp:test-sql-failover.database.windows.net,1433 | MyDatabase

Code calling the stored procedure:

    public async Task ExecuteAsync(SqlConnection connection)
    {
        const string storedProcedureName = "[dbo].[uspFooBar]";

        var command = new CommandDefinition(
            storedProcedureName,
            commandType: CommandType.StoredProcedure);

        await connection.ExecuteAsync(command);
    }

Packages used:

<PackageReference Include="Azure.Storage.Blobs" Version="12.8.0" />
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.25" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.10" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />

Startup.cs

_ = builder
         .Services
               .AddApplicationInsightsTelemetry()
                   .ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) => { module.EnableSqlCommandTextInstrumentation = true; });

Solution

  • I can got command like this, I used the same function and configuration as the one I used to test in your previous question, I think you can also get the same result.

    enter image description here

    This is my function code:

    var str = Environment.GetEnvironmentVariable("sqldb_connection");
                using (SqlConnection conn = new SqlConnection(str))
                {
                    conn.Open();
    
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    cmd.CommandText = "tiny_test_update";
                    cmd.ExecuteNonQuery();
                }
    

    By the way, the sql sentence in stored procedure can't be collected here, because the app insights gather data from function, but the function only knows the name of the stored procedure, not the details about stored procedure.