PerformanceCountersFactory.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Diagnostics
  5. {
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.Runtime;
  9. static class PerformanceCountersFactory
  10. {
  11. static internal ServicePerformanceCountersBase CreateServiceCounters(ServiceHostBase serviceHost)
  12. {
  13. if (!CheckPermissions())
  14. {
  15. return null;
  16. }
  17. if (OSEnvironmentHelper.IsVistaOrGreater)
  18. {
  19. try
  20. {
  21. var counters = new ServicePerformanceCountersV2(serviceHost);
  22. // Workaround Sys.Diag.PerformanceData problem:
  23. // Ensure that all three categories are initialized so other processes can still
  24. // expose endpoint/operation perf counters event if this one doesn't
  25. EndpointPerformanceCountersV2.EnsureCounterSet();
  26. OperationPerformanceCountersV2.EnsureCounterSet();
  27. return counters;
  28. }
  29. #pragma warning suppress 56500 // covered by FxCOP
  30. catch (Exception e)
  31. {
  32. if (Fx.IsFatal(e))
  33. {
  34. throw;
  35. }
  36. PerformanceCounters.Scope = PerformanceCounterScope.Off;
  37. if (DiagnosticUtility.ShouldTraceError)
  38. {
  39. TraceUtility.TraceEvent(TraceEventType.Error,
  40. TraceCode.PerformanceCountersFailedForService,
  41. SR.GetString(SR.TraceCodePerformanceCountersFailedForService),
  42. null, e);
  43. }
  44. return null;
  45. }
  46. }
  47. return new ServicePerformanceCounters(serviceHost);
  48. }
  49. static internal EndpointPerformanceCountersBase CreateEndpointCounters(string service, string contract, string uri)
  50. {
  51. if (!CheckPermissions())
  52. {
  53. return null;
  54. }
  55. if (OSEnvironmentHelper.IsVistaOrGreater)
  56. {
  57. try
  58. {
  59. return new EndpointPerformanceCountersV2(service, contract, uri);
  60. }
  61. #pragma warning suppress 56500 // covered by FxCOP
  62. catch (Exception e)
  63. {
  64. if (Fx.IsFatal(e))
  65. {
  66. throw;
  67. }
  68. PerformanceCounters.Scope = PerformanceCounterScope.Off;
  69. if (DiagnosticUtility.ShouldTraceError)
  70. {
  71. TraceUtility.TraceEvent(TraceEventType.Error,
  72. TraceCode.PerformanceCountersFailedForService,
  73. SR.GetString(SR.TraceCodePerformanceCountersFailedForService),
  74. null, e);
  75. }
  76. return null;
  77. }
  78. }
  79. return new EndpointPerformanceCounters(service, contract, uri);
  80. }
  81. static internal OperationPerformanceCountersBase CreateOperationCounters(string service, string contract, string operationName, string uri)
  82. {
  83. if (!CheckPermissions())
  84. {
  85. return null;
  86. }
  87. if (OSEnvironmentHelper.IsVistaOrGreater)
  88. {
  89. try
  90. {
  91. return new OperationPerformanceCountersV2(service, contract, operationName, uri);
  92. }
  93. #pragma warning suppress 56500 // covered by FxCOP
  94. catch (Exception e)
  95. {
  96. if (Fx.IsFatal(e))
  97. {
  98. throw;
  99. }
  100. PerformanceCounters.Scope = PerformanceCounterScope.Off;
  101. if (DiagnosticUtility.ShouldTraceError)
  102. {
  103. TraceUtility.TraceEvent(TraceEventType.Error,
  104. TraceCode.PerformanceCountersFailedForService,
  105. SR.GetString(SR.TraceCodePerformanceCountersFailedForService),
  106. null, e);
  107. }
  108. return null;
  109. }
  110. }
  111. return new OperationPerformanceCounters(service, contract, operationName, uri);
  112. }
  113. /// <summary>
  114. /// Returns true if we're running in full trust. Otherwise turns off performance counters and returns false.
  115. /// </summary>
  116. private static bool CheckPermissions()
  117. {
  118. // At this time (.net 4.5), performance counters require Unrestricted permissions to be created.
  119. if (PartialTrustHelpers.AppDomainFullyTrusted)
  120. {
  121. return true;
  122. }
  123. PerformanceCounters.Scope = PerformanceCounterScope.Off;
  124. if (DiagnosticUtility.ShouldTraceWarning)
  125. {
  126. TraceUtility.TraceEvent(TraceEventType.Warning,
  127. TraceCode.PerformanceCountersFailedForService,
  128. SR.GetString(SR.PartialTrustPerformanceCountersNotEnabled));
  129. }
  130. return false;
  131. }
  132. }
  133. }