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


Credo | Leadership team | Joe Sheredy

 

Joe Sheredy

VP Systems Engineering
Joe Sheredy brings over 30 years of experience in the data storage market in both HDD/SSD development as well as the IC industry supporting those products. Joe is Credo’s Vice President of Systems Engineering. Prior to joining the Credo team, Joe was with Marvell Semiconductor as Vice President of Systems Engineering, Storage Business Unit. He as a BSEL in Electronics Engineering from California Polytechnic State University, San Luis Obispo.