SQL监视和感叹

时间:2010-09-13 17:08:45

标签: c# sql sql-server entity-framework-4 extended-events

从另一个提供有关如何向SQL Server添加监视信息的线程...

See SQL From EntityFramework with Collection-like Queries

这很有效,但我想更进一步。我希望能够在发生交易时将注释添加到日志中。

我希望能够将注释写入到事物中,这将进入SQL日志记录。所以,我可以做一个查询,并调用Debugger.Logger.Write(“某种评论”)所以我知道什么是什么。我不太了解SQL,所以我试图了解正在运行的是什么,以及在哪里。

非常感谢任何帮助。我认为我必须运行另一个SQL查询以将注释“插入”查询流。

我设计了我的数据上下文(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();
        }
    }

然后是Debugger类..

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;
    }
}

我基本上已将以下SQL插入到C#...

中的资源中

这是“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

然后我有另一个,“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;

我的第一个想法是制作这样的方法..

    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();
    }

它应该像查询一样执行评论,这应该出现在评论中,对吧?好吧,那没用。所以我对其他想法持开放态度......

1 个答案:

答案 0 :(得分:1)

您可以使用PRINT语句。将参数化查询与名为@Comment的参数一起使用,然后执行

PRINT @Comment

如果评论需要与常规查询轻松区分,则可以添加新事件

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

并改为使用RAISERROR( @Comment,1,1)