You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
7.0 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2009, 2018, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Diagnostics;
  25. using System.Linq;
  26. using System.Reflection;
  27. namespace Externals.MySql.Data.MySqlClient
  28. {
  29. /// <summary>
  30. /// Traces information about the client execution.
  31. /// </summary>
  32. internal class MySqlTrace
  33. {
  34. private static TraceSource source = new TraceSource("mysql");
  35. static MySqlTrace()
  36. {
  37. foreach (TraceListener listener in source.Listeners.Cast<TraceListener>().Where(listener => listener.GetType().ToString().Contains("MySql.EMTrace.EMTraceListener")))
  38. {
  39. QueryAnalysisEnabled = true;
  40. break;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets the list of trace listeners.
  45. /// </summary>
  46. public static TraceListenerCollection Listeners { get; } = source.Listeners;
  47. /// <summary>
  48. /// Gets or sets the switch to control tracing and debugging.
  49. /// </summary>
  50. public static SourceSwitch Switch
  51. {
  52. get { return source.Switch; }
  53. set { source.Switch = value; }
  54. }
  55. /// <summary>
  56. /// Gets or sets a flag indicating if query analysis is enabled.
  57. /// </summary>
  58. public static bool QueryAnalysisEnabled { get; set; }
  59. /// <summary>
  60. /// Enables query analysis.
  61. /// </summary>
  62. /// <param name="host">The host on which to enable query analysis.</param>
  63. /// <param name="postInterval">The interval of time for logging trace information.</param>
  64. public static void EnableQueryAnalyzer(string host, int postInterval)
  65. {
  66. if (QueryAnalysisEnabled) return;
  67. // create a EMTraceListener and add it to our source
  68. TraceListener l = (TraceListener)Activator.CreateInstance(Type.GetType("MySql.EMTrace.EMTraceListener"), host, postInterval);
  69. if (l == null)
  70. throw new MySqlException(Resources.UnableToEnableQueryAnalysis);
  71. source.Listeners.Add(l);
  72. Switch.Level = SourceLevels.All;
  73. }
  74. /// <summary>
  75. /// Disables query analysis.
  76. /// </summary>
  77. public static void DisableQueryAnalyzer()
  78. {
  79. QueryAnalysisEnabled = false;
  80. foreach (TraceListener l in from TraceListener l in Source.Listeners where l.GetType().ToString().Contains("EMTraceListener") select l)
  81. {
  82. source.Listeners.Remove(l);
  83. break;
  84. }
  85. }
  86. internal static TraceSource Source
  87. {
  88. get
  89. {
  90. return source;
  91. }
  92. }
  93. internal static void LogInformation(int id, string msg)
  94. {
  95. Source.TraceEvent(TraceEventType.Information, id, msg, MySqlTraceEventType.NonQuery, -1);
  96. Trace.TraceInformation(msg);
  97. }
  98. internal static void LogWarning(int id, string msg)
  99. {
  100. Source.TraceEvent(TraceEventType.Warning, id, msg, MySqlTraceEventType.NonQuery, -1);
  101. Trace.TraceWarning(msg);
  102. }
  103. internal static void LogError(int id, string msg)
  104. {
  105. Source.TraceEvent(TraceEventType.Error, id, msg, MySqlTraceEventType.NonQuery, -1);
  106. Trace.TraceError(msg);
  107. }
  108. internal static void TraceEvent(TraceEventType eventType,
  109. MySqlTraceEventType mysqlEventType, string msgFormat, params object[] args)
  110. {
  111. Source.TraceEvent(eventType, (int)mysqlEventType, msgFormat, args);
  112. }
  113. }
  114. /// <summary>
  115. /// Specifies the types of warning flags.
  116. /// </summary>
  117. internal enum UsageAdvisorWarningFlags
  118. {
  119. /// <summary>
  120. /// No index exists.
  121. /// </summary>
  122. NoIndex = 1,
  123. /// <summary>
  124. /// Bad index exists.
  125. /// </summary>
  126. BadIndex,
  127. /// <summary>
  128. /// Rows have been excluded from the result.
  129. /// </summary>
  130. SkippedRows,
  131. /// <summary>
  132. /// Columns have been excluded from the result.
  133. /// </summary>
  134. SkippedColumns,
  135. /// <summary>
  136. /// Type conversions took place.
  137. /// </summary>
  138. FieldConversion
  139. }
  140. /// <summary>
  141. /// Specifies the event that triggered the trace.
  142. /// </summary>
  143. internal enum MySqlTraceEventType : int
  144. {
  145. /// <summary>
  146. /// A connection has been opened.
  147. /// </summary>
  148. ConnectionOpened = 1,
  149. /// <summary>
  150. /// A connection has been closed.
  151. /// </summary>
  152. ConnectionClosed,
  153. /// <summary>
  154. /// A query has been executed.
  155. /// </summary>
  156. QueryOpened,
  157. /// <summary>
  158. /// Data has been retrieved from the resultset.
  159. /// </summary>
  160. ResultOpened,
  161. /// <summary>
  162. /// Data retrieval has ended.
  163. /// </summary>
  164. ResultClosed,
  165. /// <summary>
  166. /// Query execution has ended.
  167. /// </summary>
  168. QueryClosed,
  169. /// <summary>
  170. /// The statement to be executed has been created.
  171. /// </summary>
  172. StatementPrepared,
  173. /// <summary>
  174. /// The statement has been executed.
  175. /// </summary>
  176. StatementExecuted,
  177. /// <summary>
  178. /// The statement is no longer required.
  179. /// </summary>
  180. StatementClosed,
  181. /// <summary>
  182. /// The query provided is of a nonquery type.
  183. /// </summary>
  184. NonQuery,
  185. /// <summary>
  186. /// Usage advisor warnings have been requested.
  187. /// </summary>
  188. UsageAdvisorWarning,
  189. /// <summary>
  190. /// Noncritical problem.
  191. /// </summary>
  192. Warning,
  193. /// <summary>
  194. /// An error has been raised during data retrieval.
  195. /// </summary>
  196. Error,
  197. /// <summary>
  198. /// The query has been normalized.
  199. /// </summary>
  200. QueryNormalized
  201. }
  202. }
  203. #endif