SystemDiagnosticsPerformanceCountersExtension.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. static class SystemDiagnosticsPerformanceCountersExtension
  9. {
  10. static internal void Increment(this PerformanceCountersBase thisPtr, PerformanceCounter[] counters, int counterIndex)
  11. {
  12. PerformanceCounter counter = null;
  13. try
  14. {
  15. if (counters != null)
  16. {
  17. counter = counters[counterIndex];
  18. if (counter != null)
  19. {
  20. counter.Increment();
  21. }
  22. }
  23. }
  24. #pragma warning suppress 56500 // covered by FxCOP
  25. catch (Exception e)
  26. {
  27. if (Fx.IsFatal(e)) throw;
  28. PerformanceCounters.TracePerformanceCounterUpdateFailure(thisPtr.InstanceName, thisPtr.CounterNames[counterIndex]);
  29. if (counters != null)
  30. {
  31. counters[counterIndex] = null;
  32. PerformanceCounters.ReleasePerformanceCounter(ref counter);
  33. }
  34. }
  35. }
  36. static internal void IncrementBy(this PerformanceCountersBase thisPtr, PerformanceCounter[] counters, int counterIndex, long time)
  37. {
  38. PerformanceCounter counter = null;
  39. try
  40. {
  41. if (counters != null)
  42. {
  43. counter = counters[counterIndex];
  44. if (counter != null)
  45. {
  46. counter.IncrementBy(time);
  47. }
  48. }
  49. }
  50. #pragma warning suppress 56500 // covered by FxCOP
  51. catch (Exception e)
  52. {
  53. if (Fx.IsFatal(e)) throw;
  54. PerformanceCounters.TracePerformanceCounterUpdateFailure(thisPtr.InstanceName, thisPtr.CounterNames[counterIndex]);
  55. if (counters != null)
  56. {
  57. counters[counterIndex] = null;
  58. PerformanceCounters.ReleasePerformanceCounter(ref counter);
  59. }
  60. }
  61. }
  62. static internal void Set(this PerformanceCountersBase thisPtr, PerformanceCounter[] counters, int counterIndex, long value)
  63. {
  64. PerformanceCounter counter = null;
  65. try
  66. {
  67. if (counters != null)
  68. {
  69. counter = counters[counterIndex];
  70. if (counter != null)
  71. {
  72. counter.RawValue = value;
  73. }
  74. }
  75. }
  76. #pragma warning suppress 56500 // covered by FxCOP
  77. catch (Exception e)
  78. {
  79. if (Fx.IsFatal(e)) throw;
  80. PerformanceCounters.TracePerformanceCounterUpdateFailure(thisPtr.InstanceName, thisPtr.CounterNames[counterIndex]);
  81. counters[counterIndex] = null;
  82. PerformanceCounters.ReleasePerformanceCounter(ref counter);
  83. }
  84. }
  85. static internal void Decrement(this PerformanceCountersBase thisPtr, PerformanceCounter[] counters, int counterIndex)
  86. {
  87. PerformanceCounter counter = null;
  88. try
  89. {
  90. if (counters != null)
  91. {
  92. counter = counters[counterIndex];
  93. if (counter != null)
  94. {
  95. counter.Decrement();
  96. }
  97. }
  98. }
  99. #pragma warning suppress 56500 // covered by FxCOP
  100. catch (Exception e)
  101. {
  102. if (Fx.IsFatal(e)) throw;
  103. PerformanceCounters.TracePerformanceCounterUpdateFailure(thisPtr.InstanceName, thisPtr.CounterNames[counterIndex]);
  104. if (counters != null)
  105. {
  106. counters[counterIndex] = null;
  107. PerformanceCounters.ReleasePerformanceCounter(ref counter);
  108. }
  109. }
  110. }
  111. }
  112. }