private static void Dispose(PerformanceCounter counter) { if (null != counter) { counter.RemoveInstance(); counter.Dispose(); } }
public void GetPerformanceCounter() { var counter = new System.Diagnostics.PerformanceCounter { CategoryName = "Distracey.Examples.Website-ApiFilter", InstanceName = "Default", CounterName = "ValuesController.Get(Int32 id) - GET - Average seconds taken to execute", ReadOnly = false, InstanceLifetime = PerformanceCounterInstanceLifetime.Process, }; counter.RawValue = 0; const int value = 100; counter.IncrementBy(value); System.Diagnostics.PerformanceCounter.CloseSharedResources(); Assert.AreEqual(value, counter.RawValue); counter.RemoveInstance(); counter.Dispose(); }
internal static void ReleasePerformanceCounter(ref PerformanceCounter counter) { if (counter != null) { try { counter.RemoveInstance(); counter = null; } #pragma warning suppress 56500 // covered by FxCOP catch (Exception e) { if (Fx.IsFatal(e)) { throw; } } } }
static internal PerformanceCounter GetPerformanceCounterInternal(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime) { PerformanceCounter counter = null; try { counter = new PerformanceCounter(); counter.CategoryName = categoryName; counter.CounterName = perfCounterName; counter.InstanceName = instanceName; counter.ReadOnly = false; counter.InstanceLifetime = instanceLifetime; // We now need to access the counter raw data to // force the counter object to be initialized. This // will force any exceptions due to mis-installation // of counters to occur here and be traced appropriately. try { long rawValue = counter.RawValue; } catch (InvalidOperationException) { counter = null; throw; } catch (SecurityException securityException) { // Cannot access performance counter due to partial trust scenarios // Disable the default performance counters' access otherwise // in PT the service will be broken PerformanceCounters.scope = PerformanceCounterScope.Off; DiagnosticUtility.TraceHandledException(new SecurityException(SR.GetString( SR.PartialTrustPerformanceCountersNotEnabled), securityException), TraceEventType.Warning); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityException(SR.GetString( SR.PartialTrustPerformanceCountersNotEnabled))); } } #pragma warning suppress 56500 // covered by FxCOP catch (Exception e) { if (Fx.IsFatal(e)) { throw; } if (null != counter) { if (!counter.ReadOnly) { try { counter.RemoveInstance(); } // Already inside a catch block for a failure case // ok to ---- any exceptions here and trace the // original failure. #pragma warning suppress 56500 // covered by FxCOP catch (Exception e1) { if (Fx.IsFatal(e1)) { throw; } } } counter = null; } bool logEvent = true; if (categoryName == PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters) { if (serviceOOM == false) { serviceOOM = true; } else { logEvent = false; } } else if (categoryName == PerformanceCounterStrings.SERVICEMODELOPERATION.OperationPerfCounters) { if (operationOOM == false) { operationOOM = true; } else { logEvent = false; } } else if (categoryName == PerformanceCounterStrings.SERVICEMODELENDPOINT.EndpointPerfCounters) { if (endpointOOM == false) { endpointOOM = true; } else { logEvent = false; } } if (logEvent) { DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.PerformanceCounter, (uint)System.Runtime.Diagnostics.EventLogEventId.FailedToLoadPerformanceCounter, categoryName, perfCounterName, e.ToString()); } } return counter; }
/// <summary> /// Dispose performance counter instance /// </summary> public void Dispose() { if (_initialized) { lock (_lockObject) { // If performance counter instance exists, remove it here. if (_initialized) { // We can assume here that performance counter catagory, instance and first counter in the cointerList exist as _initialized is set to true. using (PerformanceCounter pcRemove = new PerformanceCounter()) { pcRemove.CategoryName = PerformanceCounters.ShardManagementPerformanceCounterCategory; pcRemove.CounterName = counterList.First().CounterDisplayName; pcRemove.InstanceName = _instanceName; pcRemove.InstanceLifetime = PerformanceCounterInstanceLifetime.Process; pcRemove.ReadOnly = false; // Removing instance using a single counter removes all counters for that instance. pcRemove.RemoveInstance(); } } _initialized = false; } } }
private PerformanceCounter ObjectsInReferenceSystemCounterFor(IObjectContainer container) { if (_objectsCounter == null) { _objectsCounter = Db4oPerformanceCounters.CounterFor(PerformanceCounterSpec.ObjectReferenceCount, container, false); var eventRegistry = EventRegistryFactory.ForObjectContainer(container); eventRegistry.Closing += delegate { _objectsCounter.RemoveInstance(); _objectsCounter.Dispose(); }; } return _objectsCounter; }