You can use the MySqlMonitor object. It has the TraceEvent event which allows logging the required command. For this you should catch such events in the event handler, that have MonitorEventArgs.EventType with the MonitorEventType.Error value. MySqlMonitor does not have the functionality of displaying the query with parameter values instead of their names. You will have to implement such code yourself.
This is an approximate code implementing this feature (except for the query text containing parameter values):
Code: Select all
static void function()
{
MySqlMonitor mon = new MySqlMonitor();
mon.IsActive = true;
mon.TraceEvent += new Devart.Common.MonitorEventHandler(mon_TraceEvent);
MySqlConnection conn = new MySqlConnection("your connection string");
conn.Open();
MySqlCommand comm = new MySqlCommand("INSERT (field1,field2) VALUES (:p1. :p2) INTO mytable", conn);
comm.Parameters.Add(":p1", DateTime.Now);
comm.Parameters.Add(":p2", someObject.Foobar);
try
{
comm.ExecuteNonQuery();
}
catch (MySqlException ex){}
conn.Close();
}
static void mon_TraceEvent(object sender, Devart.Common.MonitorEventArgs e)
{
if (e.EventType == Devart.Common.MonitorEventType.Error && (sender.GetType() == typeof(MySqlCommand)))
{
//Your code for query text processing and logging
}
}