views:

71

answers:

1

From another thread that gave me the information on how to add monitoring to an SQL Server...

http://stackoverflow.com/questions/3586736/see-sql-from-entityframework-with-collection-like-queries

This works well, but I want to take it a step further. I'd like to be able to add comments into the log when Transactions are happening.

I want to be able to write comments into things, though, that will go into the SQL Logging. So like, I can do a query, and call in Debugger.Logger.Write("Some kind of comment") so I know what is what. I do not know SQL very well, so I am trying to understand what is being run, and where.

Any help is greatly appreciated. I presume I would have to run another SQL query to 'insert' the comment into the query stream.

I design my data context (DbContext)

using(var context = new SampleDataContext(dbModel))
{
 // ...
            // prepare a logging model.
            if (Debugger.SetupLog(context))
                Console.WriteLine("Logging Enabled...");
            // open up the debugger log
            Debugger.Open();
}

public class SampleDataContext: DbContext, IDisposable
    {
        public new void Dispose()
        {
            Debugger.Log(this);

            base.Dispose();
        }
    }

Then the Debugger class..

public static class Debugger
{
    public static System.IO.TextWriter File
    {
        get;
        set;
    }
    public static void Open()
    {
        // open a file for storing SQL results from queries
        File = new System.IO.StreamWriter("results.sql", false);
    }

    public static void Write(string text)
    {
        File.WriteLine(text);
    }

    public static bool Log(SampleDataContext context)
    {
        var results = context.Database.Connection.CreateCommand();
        results.CommandText = Sql.Review;
        context.Database.Connection.Open();

        System.Data.Common.DbDataReader resultsReader;
        do
        {
            resultsReader = results.ExecuteReader();
        }
        while (resultsReader == null);

        Console.WriteLine("Discovered a Data Reader");

        // Retrieves the schema of the table.
        var dtSchema = resultsReader.GetSchemaTable();

        string strRow; // represents a full row
        // Reads the rows one by one from the SqlDataReader
        // transfers them to a string with the given separator character and
        // writes it to the file.
        while (resultsReader.Read())
        {
            Console.WriteLine("Reading Data Reader... ");

            strRow = "";
            for (int i = 0; i < resultsReader.FieldCount; i++)
            {
                strRow += resultsReader.GetValue(i).ToString();
                if (i < resultsReader.FieldCount - 1)
                {
                    strRow += " ";
                }
            }

            Sql.Text.Lexer lexer = new Sql.Text.Lexer();
            lexer.Enscribe();

            var matches = lexer.Tokenize(strRow);
            matches.ForEach(x =>
                                {
                                    strRow = strRow.Replace(x.Value, Environment.NewLine);
                                });

            File.WriteLine(strRow);
        }

        File.Close();
        context.Database.Connection.Close();

        return false;
    }

    public static bool SetupLog(SampleDataContext context)
    {
        var command = context.Database.Connection.CreateCommand();
        command.CommandText = Sql.Record;

        context.Database.Connection.Open();
        command.ExecuteNonQuery();
        context.Database.Connection.Close();

        return true;
    }
}

I basically have inserted the following SQL into a Resource in C#...

This is "Project.SQL.Record".

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='test_trace')
    DROP EVENT SESSION [test_trace] ON SERVER;
CREATE EVENT SESSION [test_trace]
ON SERVER
ADD EVENT sqlserver.sp_statement_completed(
     ACTION (package0.callstack, sqlserver.session_id, sqlserver.sql_text)
    -- WHERE (([sqlserver].[username]='Domain\Username'))
    ),
ADD EVENT sqlserver.sql_statement_completed(
     ACTION (package0.callstack, sqlserver.session_id, sqlserver.sql_text)
     --WHERE (([sqlserver].[username]='Domain\Username'))
     )
ADD TARGET package0.ring_buffer
WITH (MAX_MEMORY = 4096KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS, 
MAX_DISPATCH_LATENCY = 1 SECONDS, MAX_EVENT_SIZE = 0KB, 
MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF)

ALTER EVENT SESSION [test_trace] ON SERVER STATE = START

Then I have another one, "Project.SQL.Review".

DECLARE 
    @session_name VARCHAR(200) = 'test_trace'

SELECT 
    pivoted_data.* 
FROM 
( 
 SELECT MIN(event_name) AS event_name,
     MIN(event_timestamp) AS event_timestamp,
     unique_event_id,
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'cpu'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [cpu],
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'duration'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [duration],
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'object_id'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [object_id],
     CONVERT ( INT, MIN (
         CASE
             WHEN d_name = 'object_type'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [object_type],
     CONVERT ( DECIMAL(28,0), MIN (
         CASE
             WHEN d_name = 'reads'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [reads],
     CONVERT ( VARCHAR(MAX), MIN (
         CASE
             WHEN d_name = 'session_id'
             AND d_package IS NOT NULL
             THEN d_value
         END ) ) AS [session_id],
     CONVERT ( INT, MIN (
         CASE
             WHEN d_name = 'source_database_id'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [source_database_id],
     CAST((SELECT CONVERT ( VARCHAR(MAX), MIN (
         CASE
             WHEN d_name = 'sql_text'
             AND d_package IS NOT NULL
             THEN d_value
         END ) )  AS [processing-instruction(x)] FOR XML PATH('') ) AS XML) AS [sql_text],
     CONVERT ( DECIMAL(28,0), MIN (
         CASE
             WHEN d_name = 'writes'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [writes]
 FROM
    ( 
        SELECT 
            *, 
            CONVERT(VARCHAR(400), NULL) AS attach_activity_id 
        FROM 
        ( 
            SELECT 
                event.value('(@name)[1]', 'VARCHAR(400)') as event_name, 
                event.value('(@timestamp)[1]', 'DATETIME') as event_timestamp, 
                DENSE_RANK() OVER (ORDER BY event) AS unique_event_id, 
                n.value('(@name)[1]', 'VARCHAR(400)') AS d_name, 
                n.value('(@package)[1]', 'VARCHAR(400)') AS d_package, 
                n.value('((value)[1]/text())[1]', 'VARCHAR(MAX)') AS d_value, 
                n.value('((text)[1]/text())[1]', 'VARCHAR(MAX)') AS d_text 
            FROM 
            ( 
                SELECT 
                    ( 
                        SELECT 
                            CONVERT(xml, target_data) 
                        FROM sys.dm_xe_session_targets st 
                        JOIN sys.dm_xe_sessions s ON 
                            s.address = st.event_session_address 
                        WHERE 
                            s.name = @session_name 
                            AND st.target_name = 'ring_buffer' 
                    ) AS [x] 
                FOR XML PATH(''), TYPE 
            ) AS the_xml(x) 
            CROSS APPLY x.nodes('//event') e (event) 
            CROSS APPLY event.nodes('*') AS q (n) 
        ) AS data_data 
    ) AS activity_data 
    GROUP BY 
        unique_event_id 
) AS pivoted_data;

My first thought was to make a method like this..

    public static void WriteSql(SampleDataContext context, string text)
    {
        var command = context.Database.Connection.CreateCommand();
        command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Comment",System.Data.SqlDbType.VarChar));
        command.Parameters["@Comment"].Value = String.Format("--{0}", text);
        command.CommandText = String.Format("PRINT '{0}'", text.Replace("'", "''"));
        context.Database.Connection.Open();
        command.ExecuteNonQuery();
        context.Database.Connection.Close();
    }

It should execute the comment like a query, which should show up in the Review, right? Well, that didn't work. So I'm open to other ideas...

+1  A: 

You could probably use a PRINT statement for this. Use a parameterised query with a parameter called @Comment then execute

PRINT @Comment

If comments need to be easily distinguished from regular queries you could add a new event

,
ADD EVENT sqlserver.error_reported(
     ACTION (sqlserver.sql_text)
     WHERE (([severity]=(1))))  

and use RAISERROR( @Comment,1,1) instead.

Martin Smith
I have edited the last bit of my code to reflect your suggestion, but all it shows is the raw SQL "PRINT @Comment" in the output. Any ideas where I am going wrong?
Stacey
No. Maybe try using `varchar` rather than text as the parameter. `text` is a LOB data type so may well not work with print. If that doesn't work then I guess you could just use something like `command.CommandText = String.Format("PRINT '{0}'", text.Replace("'","''"));` as it is your code creating the comments and you don't have to worry about SQL injection.
Martin Smith
I have updated my code above in the WriteSql method and still no dice. It doesn't show anything in the Sql Output.
Stacey