Wednesday, February 15, 2023

5.12.1 Viewing MySQL Trace Information

 MySQL Connector/NET Developer Guide  /  ...  /  Viewing MySQL Trace Information

5.12.1 Viewing MySQL Trace Information

This section describes how to set up your application to view MySQL trace information.

The first thing you need to do is create a suitable app.config file for your application. For example:

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

This configuration ensures that a suitable trace source is created, along with a switch. The switch level in this case is set to Verbose to display the maximum amount of information.

Next, add logging=true to the connection string in your C# application. For example:

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."); } } }


No comments:

Post a Comment