Is there a way to send a correlation ID from C# code to SQL Server at the command level?
For instance, using x-correlation-id
is an accepted way to track a request down to all parts of the system. we are looking for a way to pass this string value to stored procedure calls in SQL Server.
I spent sometime reading thru documents and posts but I was not able to find anything useful.
Can someone please let me know if there is a way to do this? The goal is to be able to track a specific call thru all services (which we can now) and DB calls (which we cannot and looking for a solution.)

- 203
- 2
- 11
-
I am not sure what you want to achieve by passing `x-correlation-id` to store procedure. But for tracking part , you should have proper logging or analytics in the application which can tell you which stored procedure was called in context of which `x-correlation-id`. I think you don't need to track it at the SQL server command level. – Chetan Feb 15 '20 at 01:09
-
1In a micro-service architecture environment it is very helpful to track down things down to everywhere they go. Even entity framework has the concept of tagging which shows up as comment in running scripts. When you have a live environment that runs millions of transactions in an hour, and you built it in such a way that you can tag a specific action and it gives you visibility to anywhere it goes inside the system except the DB then something is missing IMHO. We can run analytics on everything except the DB. I think MS needs to add it to SQL Server. – tecfield Feb 16 '20 at 01:10
2 Answers
I know the answer here is one year later. But in case, somebody has the same question.
Since EF core 2.2, MS provides a new method called "TagWith()" which you could pass your own annotation with the EF query into SQL server. In this way, you could easily track the SQL query with the same correlation id generated in your C# code. https://learn.microsoft.com/en-us/ef/core/querying/tags
Unfortunately, this new feature is not available in EF 6. But it is not only us in this situation. If you just need a simple solution, you could check the thread here and MS documents.
If you need a more stable solution, you could check this NuGet plugin for EF 6 as well.

- 186
- 5
-
`TagWith(x)` results in `x` being added as a comment in the SQL that's run against SQL Server. That doesn't help a huge amount as you can't capture and log it within stored procedures, for example. – Rory May 31 '22 at 19:26
To pass your correlation id to SQL Server you have two options:
- explicitly pass it as a parameter to your queries & stored procedures.
- This is annoying as it requires work to change all your db calls to have a parameter like
@correlationId
, and often doesn't make sense having that parameter for simple data-retrieval queries. Perhaps you decide to only pass it for data-modification operations. - But on the positive side it's really obvious where the correlation info comes from (i.e. nobody reading the code will be confused) and doesn't require any additional db calls.
- If all your data-modification is done using stored procs I think this is a good way to go.
- This is annoying as it requires work to change all your db calls to have a parameter like
- use SQL Server's SESSION_CONTEXT(), which is a way you can set session state on a connection that can be retrieved from within stored procs etc.
- You can find a way to inject it into your db layer (e.g. this) so the session context is always set on a connection before executing your real db calls. Then within your procs/queries you get the correlation id from the SESSION_CONTEXT and write to wherever you want to store it (e.g. some log table or as a column on tables being modified)
- This can be good as you don't need to change each of your queries or procs to have the
@correlationId
parameter. - But it's often not so transparent how the session context is magically set. Also you need to be sure it's always set correctly which can be difficult with ORMs and connection pooling and other architectural complexities.
- If you're not already using stored procs for all data modification, and you can get this working with your db access layer, and you don't mind the cost of the extra db calls this is a good option.
I wish this was easier.
Another option is to not pass it to SQL Server, but instead log all your SQL calls from the tier that makes the call and include the correlation id in those logs. That's how Application Insights & .NET seems to do it by default: logging SQL calls as a dependency along with the SQL statement and the correlation id.

- 40,559
- 52
- 175
- 261