//------------------------------------------------------------------------------ // // Method: ValidateMetricName // //------------------------------------------------------------------------------ /// <summary> /// Validates that the name of the specified metric to ensure it can be registered with Windows performance counters. /// </summary> /// <param name="metric">The metric to validate the name of.</param> private void ValidateMetricName(MetricBase metric) { if (metric.Name.Length == 0) { throw new Exception("The 'Name' property of metric " + metric.GetType().FullName + " is blank."); } if (metric.Name.Length > performanceCounterMaximumNameLength) { throw new Exception("The 'Name' property of metric " + metric.GetType().FullName + " exceeds the " + performanceCounterMaximumNameLength.ToString() + " character limit imposed by Windows performance counters."); } if (metric.Name.Length != metric.Name.Trim().Length) { throw new Exception("The 'Name' property of metric " + metric.GetType().FullName + " cannot contain leading or trailing whitespace."); } if (metric.Name.Contains("\"") == true) { throw new Exception("The 'Name' property of metric " + metric.GetType().FullName + " cannot contain the '\"' character."); } foreach (char currentChar in metric.Name) { if (char.IsControl(currentChar)) { throw new Exception("The 'Name' property of metric " + metric.GetType().FullName + " cannot contain control characters."); } } }
//------------------------------------------------------------------------------ // // Method: RegisterMetric // //------------------------------------------------------------------------------ /// <summary> /// Registers the specified metric to be written to the Windows performance counters. /// </summary> /// <param name="metric">The metric to register.</param> public void RegisterMetric(MetricBase metric) { if (registeredMetrics.ContainsKey(metric.GetType()) == true) { throw new ArgumentException("Metric of type '" + metric.GetType().Name + "' has already been registered.", "metric"); } registeredMetrics.Add(metric.GetType(), metric); RegisterPerformanceCounter(metric.Name); }