/********* ** Public methods *********/ /// <summary>Get a collection by name and creates it if it doesn't exist.</summary> /// <param name="name">The name of the collection.</param> private PerformanceCounterCollection GetOrCreateCollectionByName(string name) { if (!this.Collections.TryGetValue(name, out PerformanceCounterCollection collection)) { collection = new PerformanceCounterCollection(this, name); this.Collections[name] = collection; } return(collection); }
/// <summary>Track the invocation time for a collection.</summary> /// <param name="collectionName">The name of the collection.</param> /// <param name="action">The action to execute and track.</param> public void Track(string collectionName, Action action) { if (!this.EnableTracking) { action(); return; } PerformanceCounterCollection collection = this.GetOrCreateCollectionByName(collectionName); collection.BeginTrackInvocation(); try { action(); } finally { collection.EndTrackInvocation(); } }
/// <summary>Track a single performance counter invocation in a specific collection.</summary> /// <param name="collectionName">The name of the collection.</param> /// <param name="sourceName">The name of the source.</param> /// <param name="action">The action to execute and track.</param> public void Track(string collectionName, string sourceName, Action action) { if (!this.EnableTracking) { action(); return; } PerformanceCounterCollection collection = this.GetOrCreateCollectionByName(collectionName); DateTime eventTime = DateTime.UtcNow; this.InvocationStopwatch.Reset(); this.InvocationStopwatch.Start(); try { action(); } finally { this.InvocationStopwatch.Stop(); collection.Track(sourceName, new PerformanceCounterEntry(eventTime, this.InvocationStopwatch.Elapsed.TotalMilliseconds)); } }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="parentCollection">The collection to which this performance counter belongs.</param> /// <param name="source">The name of the source.</param> public PerformanceCounter(PerformanceCounterCollection parentCollection, string source) { this.ParentCollection = parentCollection; this.Source = source; this.Entries = new Stack <PerformanceCounterEntry>(this.MaxEntries); }