Sql Server extended events and session context

Jan Vávra 446 Reputation points
2022-12-16T11:59:36.947+00:00

Hello.
I'd like to pair an application log with extended events. I have an idea: immediately after the sql connection is opened (or obtained from pool) the app will call

EXEC sys.sp_set_session_context ('appContex', 'threadId');

For extended event 'rpc_completed' I can choose these field

271412-obrazek.png

and for 'sp_statement_completed'

271338-obrazek.png

I am at a dead end ...

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2022-12-16T22:43:06.09+00:00

    If you instead use the older SET CONTEXT_INFO, you can capture that with extended events. But I am not going to say that this is a feasible solution.

    Given that session_context can be a lot bigger than context_info, I can understand why they don't make it available for extended events. But at the same time, the need to capture who is the actual user is very apparent, so I think that Microsoft needs to find a solution. Maybe a flag to sp_set_session_context that you want to track this particular session key.

    Anyway, the place to suggest improvements to SQL Server is https://feedback.azure.com/d365community/forum/04fe6ee0-3b25-ec11-b6e6-000d3a4f0da0.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.