本节介绍如何设置您的应用程序以查看 MySQL 跟踪信息。
您需要做的第一件事是
app.config
为您的应用程序创建一个合适的文件。例如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="mysql" switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="console" />
<remove name ="Default" />
</listeners>
</source>
</sources>
<switches>
<!-- You can set the level at which tracing is to occur -->
<add name="SourceSwitch" value="Verbose" />
<!-- You can turn tracing off -->
<!--add name="SourceSwitch" value="Off" -->
</switches>
<sharedListeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener"
initializeData="false"/>
</sharedListeners>
</system.diagnostics>
</configuration>
此配置可确保创建合适的跟踪源以及开关。这种情况下的开关级别设置Verbose
为显示最大信息量。
接下来,添加logging=true
到 C# 应用程序中的连接字符串。例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Web;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;logging=true";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " -- " + rdr[1]);
}
rdr.Close();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Done.");
}
}
}
这个简单的应用程序然后生成以下输出:
Connecting to MySQL...
mysql Information: 1 : 1: Connection Opened: connection string = 'server=localhost;User Id=root;database=world;port=3306
;password=******;logging=True'
mysql Information: 3 : 1: Query Opened: SHOW VARIABLES
mysql Information: 4 : 1: Resultset Opened: field(s) = 2, affected rows = -1, inserted id = -1
mysql Information: 5 : 1: Resultset Closed. Total rows=272, skipped rows=0, size (bytes)=7058
mysql Information: 6 : 1: Query Closed
mysql Information: 3 : 1: Query Opened: SHOW COLLATION
mysql Information: 4 : 1: Resultset Opened: field(s) = 6, affected rows = -1, inserted id = -1
mysql Information: 5 : 1: Resultset Closed. Total rows=127, skipped rows=0, size (bytes)=4102
mysql Information: 6 : 1: Query Closed
mysql Information: 3 : 1: Query Opened: SET character_set_results=NULL
mysql Information: 4 : 1: Resultset Opened: field(s) = 0, affected rows = 0, inserted id = 0
mysql Information: 5 : 1: Resultset Closed. Total rows=0, skipped rows=0, size (bytes)=0
mysql Information: 6 : 1: Query Closed
mysql Information: 10 : 1: Set Database: world
mysql Information: 3 : 1: Query Opened: SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'
mysql Information: 4 : 1: Resultset Opened: field(s) = 2, affected rows = -1, inserted id = -1
American Samoa -- George W. Bush
Australia -- Elisabeth II
...
Wallis and Futuna -- Jacques Chirac
Vanuatu -- John Bani
United States Minor Outlying Islands -- George W. Bush
mysql Information: 5 : 1: Resultset Closed. Total rows=28, skipped rows=0, size (bytes)=788
mysql Information: 6 : 1: Query Closed
Done.
mysql Information: 2 : 1: Connection Closed
跟踪消息中显示的第一个数字对应于 MySQL 事件类型。跟踪消息中显示的第二个数字是连接计数。下表描述了每种 MySQL 事件类型。
事件类型 | 描述 |
---|---|
1个 | ConnectionOpened:连接字符串 |
2个 | 连接关闭: |
3个 | QueryOpened:mysql服务器线程id,查询文本 |
4个 | ResultOpened:字段数,受影响的行(如果选择-1),插入的id(如果选择-1) |
5个 | ResultClosed:读取的总行数、跳过的行数、结果集的大小(以字节为单位) |
6个 | 查询关闭: |
7 | StatementPrepared:准备好的sql,语句id |
8个 | StatementExecuted:语句id,mysql服务器线程id |
9 | StatementClosed:语句 ID |
10 | 非查询:[变化] |
11 | UsageAdvisorWarning:使用顾问标志。NoIndex = 1,BadIndex = 2,SkippedRows = 3,SkippedColumns = 4,FieldConversion = 5。 |
12 | 警告:级别、代码、消息 |
13 | 错误:错误号、错误信息 |
尽管此示例使用
ConsoleTraceListener
,但可以使用任何其他标准侦听器。另一种可能性是创建一个自定义侦听器,该侦听器使用随
TraceEvent
方法传入的信息。例如,可以创建自定义跟踪侦听器来执行 MySQL 事件消息的主动监视,而不是简单地将这些消息写入输出设备。
也可以在运行时将侦听器添加到 MySQL Trace Source。这可以通过以下代码完成:
MySqlTrace.Listeners.Add(new ConsoleTraceListener());
连接器/NET 提供了在运行时打开和关闭跟踪的能力。这可以使用调用
MySqlTrace.EnableQueryAnalyzer(string host, int
postInterval)
和
来实现MySqlTrace.DisableQueryAnalyzer()
。该参数host
是要监控的 MySQL Enterprise Monitor 服务器的 URL。该参数
postInterval
是将数据发布到 MySQL Enterprise Monitor 的频率,以秒为单位。