3

For ASP.NET Core (and likely other platforms), Application Insights by default tracks all queries to Azure SQL Database (and likely other platforms) as dependency operations. For each query, it records the query string, something like this:

SELECT [b].[Foo] FROM [Bar] AS [b] WHERE [b].[Id] = @id

However, it doesn't record the value of @id. How do I make it record the values of the query parameters?

3 Answers 3

4

This is tracked as a feature request on GitHub: https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/193

We already implemented this type of extensibility for http calls where you now have access to the original http request object to extract additional details.

With the current version of SDK the best option is to set those parameters somewhere on current thread and access from telemetry initializer or track this operation manually by wrapping all SQL calls.

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

I see that the issue is closed. Is there any example on how to use it?
0

You should be able to use custom dependency tracking feature and create telemetry for SQL database dependency with the right information from code. - https://learn.microsoft.com/en-us/azure/application-insights/app-insights-asp-net-dependencies#custom-dependency-tracking

1 Comment

That sound conceptually good, but digging into the inners of innards of EF to hook into its generated SQL with parameter values seems like a big task. I was hoping for something more off-the-shelf.
0

this way you will have the entire query in the log

$"SELECT [b].[Foo] FROM [Bar] AS [b] WHERE [b].[Id] = {id}"

1 Comment

Unfortunately, that approach makes it difficult to guard against SQL injection attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.