How can I write events to the XEvent Profiler

David Thielen 2,276 Reputation points
2024-03-13T16:03:17.92+00:00

Hi all;

Is there a way to write a string to the XEvent profiler that will then put it in the profiler output? What I want to do is in my OnInitializeAsync() method (my app is Blazor Server using Entity Framework) is write to the XProfiler event list at the start, and again at the end of the method. That way I can see what SQL queries occurred in that method.

If there is no explicit way to do this, would I have any problems doing the following (it works, just asking if it's going to cause me problems)

if (debugMode) {
    await dbContext.Database.ExecuteSqlAsync("-- starting Dashboard.OnInitializeAsync()");
}

And while the above "works", it's ugly in the output (see below). So something better would be great.

exec sp_executesql N'EXEC sp_trace_generateevent @eventid = 82, @userinfo = N''@p0'';',N'@p0 nvarchar(4000)',@p0=N'Leaving Dashboard.OnInitializedAsync()'

thanks - dave

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,715 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,002 questions
{count} votes