SQL监控和插入

发布于 2024-09-19 11:19:35 字数 8374 浏览 1 评论 0原文

来自另一个线程,该线程为我提供了有关如何向 SQL Server 添加监视的信息...

使用类似于集合的查询从 EntityFramework 查看 SQL

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

不过,我希望能够将注释写入到 SQL 日志记录中。就像,我可以进行查询,然后调用 Debugger.Logger.Write("Some kind of comment") 这样我就知道什么是什么。我不太了解 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();
        }
    }

然后是调试器类..

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

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

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-09-26 11:19:35

您可能可以使用 PRINT 语句来实现此目的。使用带有名为 @Comment 的参数的参数化查询,然后执行

PRINT @Comment

如果需要轻松区分注释与常规查询,您可以添加一个新事件

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

并使用 RAISERROR( @Comment,1,1)< /代码> 相反。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文