Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As long as you're using SQL Server, and as long as you have (or can have) centralized connection-creation logic, you can use the <code>CONTEXT_INFO</code> feature: <a href="http://msdn.microsoft.com/en-us/library/ms187768.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms187768.aspx</a></p> <p>We do something like this to track DB connections in a multi-user server-side application. Every time a new DB connection is created (reused from connection pooling, really, but "created" in ADODB/ADO.Net code):</p> <pre><code>DECLARE @ContextInfoBinary binary(128); SET @ContextInfoBinary = Convert(Binary(128), 'XX' + Replicate(Char(0), 36) + 'Up to 90 characters of connection info - username, page, action, etc') SET CONTEXT_INFO @ContextInfoBinary </code></pre> <p>Later, when you want to "track" the connections, you can convert the specified range of the context-info back to VarChar content:</p> <pre><code>SELECT RTRIM(hostname) AS hostname , hostprocess , RTRIM(program_name) AS program_name , CASE WHEN SubString(Context_Info, 1, 2) = 0x5858 THEN Replace(Convert(VarChar(90), SubString(Context_Info, 39, 90)), CHAR(0), '') ELSE 'UNKNOWN' END AS ExtendedConnectionDescription --More stuff here, depending on what you're looking for FROM master.sys.sysprocesses </code></pre> <p>Some considerations:</p> <ul> <li>We moved to this concept specifically because of connection-pooling issues with the Application Name feature. It's important to us to be able to track blocking causes down to specific users and processes, at any given point in time.</li> <li>This adds a DB command to be executed for <em>every</em> connection created - this may or may not represent significant overhead, depending on your application design</li> <li>This sample uses VarChar (up to 90 characters); if you need to track NVarChar(extended character) data, your space drops to 45 characters</li> <li>In this example we're leaving 36 bytes that could be used for some other purpose, eg for preventing triggers from executing for some connections.</li> <li>You can output the value to a trace explicitly using <code>sp_trace_generateevent</code>, as outlined in this related question: <a href="https://stackoverflow.com/questions/1049656/how-do-you-access-the-context-info-variable-in-sql2005-profiler">How do you access the Context_Info() variable in SQL2005 Profiler?</a> </li> </ul> <hr> <p>UPDATE:</p> <p>Only after re-reading your question did I realize you seem to be explicitly looking to add info to Traces only, and not so much for ad-hoc analysis of your current connections (which is more what I associated the use of "Application Name" with, sorry); the only useful approach I've seen specifically is the <code>sp_trace_generateevent</code> call. If you're going to do this, I'd advise you to <strong>also</strong> add the connection_info, as it won't cost you any more (you alsready have the db round-trip for <code>sp_trace_generateevent</code>) and will definitely help you with other types of analysis later on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload