#if MYSQL_6_10 // Copyright ?2009, 2018, Oracle and/or its affiliates. All rights reserved. // // MySQL Connector/NET is licensed under the terms of the GPLv2 // , like most // MySQL Connectors. There are special exceptions to the terms and // conditions of the GPLv2 as it is applied to this software, see the // FLOSS License Exception // . // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published // by the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA using System; using System.Diagnostics; using System.Linq; using System.Reflection; namespace Externals.MySql.Data.MySqlClient { /// /// Traces information about the client execution. /// internal class MySqlTrace { private static TraceSource source = new TraceSource("mysql"); static MySqlTrace() { foreach (TraceListener listener in source.Listeners.Cast().Where(listener => listener.GetType().ToString().Contains("MySql.EMTrace.EMTraceListener"))) { QueryAnalysisEnabled = true; break; } } /// /// Gets the list of trace listeners. /// public static TraceListenerCollection Listeners { get; } = source.Listeners; /// /// Gets or sets the switch to control tracing and debugging. /// public static SourceSwitch Switch { get { return source.Switch; } set { source.Switch = value; } } /// /// Gets or sets a flag indicating if query analysis is enabled. /// public static bool QueryAnalysisEnabled { get; set; } /// /// Enables query analysis. /// /// The host on which to enable query analysis. /// The interval of time for logging trace information. public static void EnableQueryAnalyzer(string host, int postInterval) { if (QueryAnalysisEnabled) return; // create a EMTraceListener and add it to our source TraceListener l = (TraceListener)Activator.CreateInstance(Type.GetType("MySql.EMTrace.EMTraceListener"), host, postInterval); if (l == null) throw new MySqlException(Resources.UnableToEnableQueryAnalysis); source.Listeners.Add(l); Switch.Level = SourceLevels.All; } /// /// Disables query analysis. /// public static void DisableQueryAnalyzer() { QueryAnalysisEnabled = false; foreach (TraceListener l in from TraceListener l in Source.Listeners where l.GetType().ToString().Contains("EMTraceListener") select l) { source.Listeners.Remove(l); break; } } internal static TraceSource Source { get { return source; } } internal static void LogInformation(int id, string msg) { Source.TraceEvent(TraceEventType.Information, id, msg, MySqlTraceEventType.NonQuery, -1); Trace.TraceInformation(msg); } internal static void LogWarning(int id, string msg) { Source.TraceEvent(TraceEventType.Warning, id, msg, MySqlTraceEventType.NonQuery, -1); Trace.TraceWarning(msg); } internal static void LogError(int id, string msg) { Source.TraceEvent(TraceEventType.Error, id, msg, MySqlTraceEventType.NonQuery, -1); Trace.TraceError(msg); } internal static void TraceEvent(TraceEventType eventType, MySqlTraceEventType mysqlEventType, string msgFormat, params object[] args) { Source.TraceEvent(eventType, (int)mysqlEventType, msgFormat, args); } } /// /// Specifies the types of warning flags. /// internal enum UsageAdvisorWarningFlags { /// /// No index exists. /// NoIndex = 1, /// /// Bad index exists. /// BadIndex, /// /// Rows have been excluded from the result. /// SkippedRows, /// /// Columns have been excluded from the result. /// SkippedColumns, /// /// Type conversions took place. /// FieldConversion } /// /// Specifies the event that triggered the trace. /// internal enum MySqlTraceEventType : int { /// /// A connection has been opened. /// ConnectionOpened = 1, /// /// A connection has been closed. /// ConnectionClosed, /// /// A query has been executed. /// QueryOpened, /// /// Data has been retrieved from the resultset. /// ResultOpened, /// /// Data retrieval has ended. /// ResultClosed, /// /// Query execution has ended. /// QueryClosed, /// /// The statement to be executed has been created. /// StatementPrepared, /// /// The statement has been executed. /// StatementExecuted, /// /// The statement is no longer required. /// StatementClosed, /// /// The query provided is of a nonquery type. /// NonQuery, /// /// Usage advisor warnings have been requested. /// UsageAdvisorWarning, /// /// Noncritical problem. /// Warning, /// /// An error has been raised during data retrieval. /// Error, /// /// The query has been normalized. /// QueryNormalized } } #endif