OperationPerformanceCounters.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Diagnostics
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime;
  8. sealed class OperationPerformanceCounters : OperationPerformanceCountersBase
  9. {
  10. internal PerformanceCounter[] Counters { get; set; }
  11. internal OperationPerformanceCounters(string service, string contract, string operationName, string uri)
  12. : base(service, contract, operationName, uri)
  13. {
  14. this.Counters = new PerformanceCounter[(int)PerfCounters.TotalCounters];
  15. for (int i = 0; i < (int)PerfCounters.TotalCounters; i++)
  16. {
  17. PerformanceCounter counter = PerformanceCounters.GetOperationPerformanceCounter(perfCounterNames[i], this.instanceName);
  18. if (counter != null)
  19. {
  20. try
  21. {
  22. counter.RawValue = 0;
  23. this.Counters[i] = counter;
  24. }
  25. #pragma warning suppress 56500 // covered by FxCOP
  26. catch (Exception e)
  27. {
  28. if (Fx.IsFatal(e))
  29. {
  30. throw;
  31. }
  32. if (DiagnosticUtility.ShouldTraceError)
  33. {
  34. TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.PerformanceCounterFailedToLoad,
  35. SR.GetString(SR.TraceCodePerformanceCounterFailedToLoad), null, e);
  36. }
  37. break;
  38. }
  39. }
  40. else
  41. {
  42. break;
  43. }
  44. }
  45. }
  46. void Increment(int counter)
  47. {
  48. this.Increment(this.Counters, counter);
  49. }
  50. void IncrementBy(int counter, long time)
  51. {
  52. this.IncrementBy(this.Counters, counter, time);
  53. }
  54. void Decrement(int counter)
  55. {
  56. this.Decrement(this.Counters, counter);
  57. }
  58. internal override void MethodCalled()
  59. {
  60. Increment((int)PerfCounters.Calls);
  61. Increment((int)PerfCounters.CallsPerSecond);
  62. Increment((int)PerfCounters.CallsOutstanding);
  63. }
  64. internal override void MethodReturnedSuccess()
  65. {
  66. Decrement((int)PerfCounters.CallsOutstanding);
  67. }
  68. internal override void MethodReturnedError()
  69. {
  70. Increment((int)PerfCounters.CallsFailed);
  71. Increment((int)PerfCounters.CallsFailedPerSecond);
  72. Decrement((int)PerfCounters.CallsOutstanding);
  73. }
  74. internal override void MethodReturnedFault()
  75. {
  76. Increment((int)PerfCounters.CallsFaulted);
  77. Increment((int)PerfCounters.CallsFaultedPerSecond);
  78. Decrement((int)PerfCounters.CallsOutstanding);
  79. }
  80. internal override void SaveCallDuration(long time)
  81. {
  82. IncrementBy((int)PerfCounters.CallDuration, time);
  83. Increment((int)PerfCounters.CallDurationBase);
  84. }
  85. internal override void AuthenticationFailed()
  86. {
  87. Increment((int)PerfCounters.SecurityValidationAuthenticationFailures);
  88. Increment((int)PerfCounters.SecurityValidationAuthenticationFailuresPerSecond);
  89. }
  90. internal override void AuthorizationFailed()
  91. {
  92. Increment((int)PerfCounters.CallsNotAuthorized);
  93. Increment((int)PerfCounters.CallsNotAuthorizedPerSecond);
  94. }
  95. internal override void TxFlowed()
  96. {
  97. Increment((int)PerfCounters.TxFlowed);
  98. Increment((int)PerfCounters.TxFlowedPerSecond);
  99. }
  100. internal override bool Initialized
  101. {
  102. get { return this.Counters != null; }
  103. }
  104. protected override void Dispose(bool disposing)
  105. {
  106. try
  107. {
  108. if (disposing)
  109. {
  110. if (PerformanceCounters.PerformanceCountersEnabled)
  111. {
  112. if (null != this.Counters)
  113. {
  114. for (int ctr = this.PerfCounterStart; ctr < this.PerfCounterEnd; ++ctr)
  115. {
  116. PerformanceCounter counter = this.Counters[ctr];
  117. if (counter != null)
  118. {
  119. PerformanceCounters.ReleasePerformanceCounter(ref counter);
  120. }
  121. this.Counters[ctr] = null;
  122. }
  123. this.Counters = null;
  124. }
  125. }
  126. }
  127. }
  128. finally
  129. {
  130. base.Dispose(disposing);
  131. }
  132. }
  133. }
  134. }