MySQL Connector/NET 发行说明
本节介绍如何设置应用程序以查看 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 | ConnectionClosed |
3 | QueryOpened:MySQL 服务器线程 ID,查询文本 |
4 | ResultOpened:字段计数,受影响的行(如果选择则为 -1),插入的 ID(如果选择则为 -1) |
5 | ResultClosed:读取的总行数,跳过的行数,结果集的大小(以字节为单位) |
6 | QueryClosed |
7 | StatementPrepared:准备好的 SQL,语句 ID |
8 | StatementExecuted:语句 ID,MySQL 服务器线程 ID |
9 | StatementClosed:语句 ID |
10 | NonQuery: [变化] |
11 | UsageAdvisorWarning:使用顾问标志。NoIndex = 1,BadIndex = 2,SkippedRows = 3,SkippedColumns = 4,FieldConversion = 5。 |
12 | Warning:级别,代码,消息 |
13 | Error:错误号,错误消息 |
虽然此示例使用 ConsoleTraceListener
,但可以使用任何其他标准侦听器。另一种可能性是创建一个使用 TraceEvent
方法传递的信息的自定义侦听器。例如,可以创建一个自定义跟踪侦听器来对 MySQL 事件消息进行主动监控,而不仅仅是将这些消息写入输出设备。
也可以在运行时将侦听器添加到 MySQL 跟踪源中。这可以通过以下代码完成
MySqlTrace.Listeners.Add(new ConsoleTraceListener());
Connector/NET 提供在运行时打开和关闭跟踪的功能。这可以使用调用 MySqlTrace.EnableQueryAnalyzer(string host, int postInterval)
和 MySqlTrace.DisableQueryAnalyzer()
来实现。参数 host
是要监控的 MySQL Enterprise Monitor 服务器的 URL。参数 postInterval
是每隔多少秒将数据发布到 MySQL Enterprise Monitor,以秒为单位。