SqlEventSource.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SqlEventSource.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">mihailsm</owner>
  6. //------------------------------------------------------------------------------
  7. namespace System.Data
  8. {
  9. using System;
  10. using System.Diagnostics.Tracing;
  11. [EventSource(Name = SqlEventSource.EventSourceName)]
  12. internal sealed class SqlEventSource : EventSource
  13. {
  14. internal const string EventSourceName = "Microsoft-AdoNet-SystemData";
  15. /// <summary>
  16. /// Defines EventId for BeginExecute (Reader, Scalar, NonQuery, XmlReader).
  17. /// </summary>
  18. private const int BeginExecuteEventId = 1;
  19. /// <summary>
  20. /// Defines EventId for EndExecute (Reader, Scalar, NonQuery, XmlReader).
  21. /// </summary>
  22. private const int EndExecuteEventId = 2;
  23. // Defines the singleton instance for the Resources ETW provider
  24. internal static readonly SqlEventSource Log = new SqlEventSource();
  25. /// <summary>
  26. /// Keyword definitions. These represent logical groups of events that can be turned on and off independently
  27. /// Often each task has a keyword, but where tasks are determined by subsystem, keywords are determined by
  28. /// usefulness to end users to filter. Generally users don't mind extra events if they are not high volume
  29. /// so grouping low volume events together in a single keywords is OK (users can post-filter by task if desired)
  30. /// <remarks>The visibility of the enum has to be public, otherwise there will be an ArgumentException on calling related WriteEvent method.</remarks>
  31. /// </summary>
  32. public static class Keywords
  33. {
  34. public const EventKeywords SqlClient = (EventKeywords)0x0001; // This is bit 0
  35. }
  36. public static class Tasks // this name is important for EventSource
  37. {
  38. /// <summary>Task that tracks sql command execute.</summary>
  39. public const EventTask ExecuteCommand = (EventTask)1;
  40. }
  41. private SqlEventSource()
  42. {
  43. }
  44. // unfortunately these are not marked as Start/Stop opcodes. The reason is that we dont want them to participate in
  45. // the EventSource activity IDs (because they currently don't use tasks and this simply confuses the logic) and
  46. // because of versioning requirements we don't have ActivityOptions capability (because mscorlib and System.Data version
  47. // at different rates) Sigh...
  48. [Event(SqlEventSource.BeginExecuteEventId, Keywords = Keywords.SqlClient)]
  49. public void BeginExecute(int objectId, string dataSource, string database, string commandText)
  50. {
  51. // we do not use unsafe code for better performance optization here because optimized helpers make the code unsafe where that would not be the case otherwise.
  52. // This introduces the question of partial trust, which is complex in the SQL case (there are a lot of scenarios and SQL has special security support).
  53. WriteEvent(SqlEventSource.BeginExecuteEventId, objectId, dataSource, database, commandText);
  54. }
  55. // unfortunately these are not marked as Start/Stop opcodes. The reason is that we dont want them to participate in
  56. // the EventSource activity IDs (because they currently don't use tasks and this simply confuses the logic) and
  57. // because of versioning requirements we don't have ActivityOptions capability (because mscorlib and System.Data version
  58. // at different rates) Sigh...
  59. [Event(SqlEventSource.EndExecuteEventId, Keywords = Keywords.SqlClient)]
  60. public void EndExecute(int objectId, int compositeState, int sqlExceptionNumber)
  61. {
  62. WriteEvent(SqlEventSource.EndExecuteEventId, objectId, compositeState, sqlExceptionNumber);
  63. }
  64. }
  65. }