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.

153 lines
4.7 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2009, 2013, 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.Collections.Generic;
  25. using System.Text;
  26. using System.Diagnostics;
  27. using System.Reflection;
  28. using Externals.MySql.Data.MySqlClient.Properties;
  29. namespace Externals.MySql.Data.MySqlClient
  30. {
  31. internal sealed partial class MySqlTrace
  32. {
  33. private static TraceSource source = new TraceSource("mysql");
  34. // static string qaHost;
  35. static bool qaEnabled = false;
  36. static MySqlTrace()
  37. {
  38. foreach (TraceListener listener in source.Listeners)
  39. {
  40. if (listener.GetType().ToString().Contains("MySql.EMTrace.EMTraceListener"))
  41. {
  42. qaEnabled = true;
  43. break;
  44. }
  45. }
  46. }
  47. public static TraceListenerCollection Listeners
  48. {
  49. get { return source.Listeners; }
  50. }
  51. public static SourceSwitch Switch
  52. {
  53. get { return source.Switch; }
  54. set { source.Switch = value; }
  55. }
  56. public static bool QueryAnalysisEnabled
  57. {
  58. get { return qaEnabled; }
  59. }
  60. public static void EnableQueryAnalyzer(string host, int postInterval)
  61. {
  62. if (qaEnabled) return;
  63. // create a EMTraceListener and add it to our source
  64. // TraceListener l = (TraceListener)Activator.CreateInstance("MySql.EMTrace", "MySql.EMTrace.EMTraceListener", false, BindingFlags.CreateInstance, null, new object[] { host, postInterval }, null, null, null).Unwrap();
  65. TraceListener l = null;
  66. if (l == null) throw new MySqlException(Resources.UnableToEnableQueryAnalysis);
  67. source.Listeners.Add(l);
  68. Switch.Level = SourceLevels.All;
  69. }
  70. public static void DisableQueryAnalyzer()
  71. {
  72. qaEnabled = false;
  73. foreach (TraceListener l in source.Listeners)
  74. if (l.GetType().ToString().Contains("EMTraceListener"))
  75. {
  76. source.Listeners.Remove(l);
  77. break;
  78. }
  79. }
  80. internal static TraceSource Source
  81. {
  82. get { return source; }
  83. }
  84. internal static void LogInformation(int id, string msg)
  85. {
  86. Source.TraceEvent(TraceEventType.Information, id, msg, MySqlTraceEventType.NonQuery, -1);
  87. Trace.TraceInformation(msg);
  88. }
  89. internal static void LogWarning(int id, string msg)
  90. {
  91. Source.TraceEvent(TraceEventType.Warning, id, msg, MySqlTraceEventType.NonQuery, -1);
  92. Trace.TraceWarning(msg);
  93. }
  94. internal static void LogError(int id, string msg)
  95. {
  96. Source.TraceEvent(TraceEventType.Error, id, msg, MySqlTraceEventType.NonQuery, -1);
  97. Trace.TraceError(msg);
  98. }
  99. internal static void TraceEvent(TraceEventType eventType,
  100. MySqlTraceEventType mysqlEventType, string msgFormat, params object[] args)
  101. {
  102. Source.TraceEvent(eventType, (int)mysqlEventType, msgFormat, args);
  103. }
  104. }
  105. internal enum UsageAdvisorWarningFlags
  106. {
  107. NoIndex = 1,
  108. BadIndex,
  109. SkippedRows,
  110. SkippedColumns,
  111. FieldConversion
  112. }
  113. internal enum MySqlTraceEventType : int
  114. {
  115. ConnectionOpened = 1,
  116. ConnectionClosed,
  117. QueryOpened,
  118. ResultOpened,
  119. ResultClosed,
  120. QueryClosed,
  121. StatementPrepared,
  122. StatementExecuted,
  123. StatementClosed,
  124. NonQuery,
  125. UsageAdvisorWarning,
  126. Warning,
  127. Error,
  128. QueryNormalized
  129. }
  130. }
  131. #endif