private Boolean AddPerformanceCounter(string category, string names, string instance, double multiplier, int decimalPlaces, string collectdPlugin, string collectdPluginInstance, string collectdType, string collectdTypeInstance) { string logstr = string.Format(new FixedLengthFormatter(), "Category={0} Counter={1} Instance={2} CollectdPlugin={3} CollectdPluginInstance={4} CollectdType={5} CollectdTypeInstance={6} Multiplier={7} DecimalPlaces={8}", category, names, instance, collectdPlugin, collectdPluginInstance, collectdType, collectdTypeInstance, multiplier, decimalPlaces); try { var metric = new Metric(); string[] counterList = names.Split(','); metric.Counters = new List<PerformanceCounter>(); metric.FriendlyNames = new string[counterList.Length]; int ix = 0; foreach (string ctr in counterList) { PerformanceCounter perfCounter = new PerformanceCounter(category, ctr.Trim(), instance); // Collect a value - this is needed to initialise counters that need two values perfCounter.NextValue(); metric.Counters.Add(perfCounter); string friendlyName = ctr.Trim(); if (instance.Length > 0) friendlyName += " (" + instance + ")"; metric.FriendlyNames[ix++] = friendlyName; } metric.Category = category; metric.Instance = instance; metric.CounterName = names; metric.Multiplier = multiplier; metric.DecimalPlaces = decimalPlaces; metric.CollectdPlugin = collectdPlugin; metric.CollectdPluginInstance = collectdPluginInstance; metric.CollectdType = collectdType; metric.CollectdTypeInstance = collectdTypeInstance; _metrics.Add(metric); Logger.Debug("Added Performance counter : {0}", logstr); return true; } catch (InvalidOperationException ex) { if (ex.Message.ToLower().Contains("category does not exist")) { Logger.Warn(string.Format("Performance Counter not added: Category does not exist: {0}", category)); } else { LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Could not initialise performance counter"); logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION); Logger.Log(logEvent); Logger.Warn(string.Format("Could not initialise performance counter category: {0}, instance: {1}, counter: {2}", category, instance, names), ex); return false; } return false; } catch (Exception ex) { LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Could not initialise performance counter"); logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION); Logger.Log(logEvent); Logger.Warn(string.Format("Could not initialise performance counter category: {0}, instance: {1}, counter: {2}", category, instance, names), ex); return false; } }
private void AddPerformanceCounter(string category, string names, string instance, uint scaleUpFactor, uint scaleDownFactor, string collectdPlugin, string collectdPluginInstance, string collectdType, string collectdTypeInstance) { string logstr = string.Format( "Category:{0} - Instance:{1} - counter:{2} - ScaleUpFactor:{3} - ScaleDownFactor:{4} - CollectdPlugin:{5} - CollectdPluginInstance:{6} - CollectdType:{7} - CollectdTypeInstance:{8}", category, instance, names, scaleUpFactor, scaleDownFactor, collectdPlugin, collectdPluginInstance, collectdType, collectdTypeInstance); try { var metric = new Metric(); string[] counterList = names.Split(','); metric.Counters = new List<PerformanceCounter>(); foreach (string ctr in counterList) metric.Counters.Add(new PerformanceCounter(category, ctr.Trim(), instance)); metric.Category = category; metric.Instance = instance; metric.CounterName = names; metric.ScaleUpFactor = scaleUpFactor; metric.ScaleDownFactor = scaleDownFactor; metric.CollectdPlugin = collectdPlugin; metric.CollectdPluginInstance = collectdPluginInstance; metric.CollectdType = collectdType; metric.CollectdTypeInstance = collectdTypeInstance; _metrics.Add(metric); Logger.Info("Added Performance COUNTER : {0}", logstr); } catch (Exception exp) { Logger.Error("Got exception : {0}, while adding performance counter: {1}", exp, logstr); } }
private Boolean AddPerformanceCounter(string category, string names, string instance, double multiplier, int decimalPlaces, string collectdPlugin, string collectdPluginInstance, string collectdType, string collectdTypeInstance) { string logstr = string.Format(new FixedLengthFormatter(), "Category={0} Counter={1} Instance={2} CollectdPlugin={3} CollectdPluginInstance={4} CollectdType={5} CollectdTypeInstance={6} Multiplier={7} DecimalPlaces={8}", category, names, instance, collectdPlugin, collectdPluginInstance, collectdType, collectdTypeInstance, multiplier, decimalPlaces); try { var metric = new Metric(); string[] counterList = names.Split(','); metric.Counters = new List <PerformanceCounter>(); metric.FriendlyNames = new string[counterList.Length]; int ix = 0; foreach (string ctr in counterList) { PerformanceCounter perfCounter = new PerformanceCounter(category, ctr.Trim(), instance); // Collect a value - this is needed to initialise counters that need two values perfCounter.NextValue(); metric.Counters.Add(perfCounter); string friendlyName = ctr.Trim(); if (instance.Length > 0) { friendlyName += " (" + instance + ")"; } metric.FriendlyNames[ix++] = friendlyName; } metric.Category = category; metric.Instance = instance; metric.CounterName = names; metric.Multiplier = multiplier; metric.DecimalPlaces = decimalPlaces; metric.CollectdPlugin = collectdPlugin; metric.CollectdPluginInstance = collectdPluginInstance.Replace(".", "_");// Don't allow dots in PluginInstance name - these can be a problem with network interface names metric.CollectdType = collectdType; metric.CollectdTypeInstance = collectdTypeInstance; _metrics.Add(metric); Logger.Debug("Added Performance counter : {0}", logstr); return(true); } catch (InvalidOperationException ex) { if (ex.Message.ToLower().Contains("category does not exist")) { Logger.Warn(string.Format("Performance Counter not added: Category does not exist: {0}", category)); } else { LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Could not initialise performance counter"); logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION); Logger.Log(logEvent); Logger.Warn(string.Format("Could not initialise performance counter category: {0}, instance: {1}, counter: {2}", category, instance, names), ex); return(false); } return(false); } catch (Exception ex) { LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Could not initialise performance counter"); logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION); Logger.Log(logEvent); Logger.Warn(string.Format("Could not initialise performance counter category: {0}, instance: {1}, counter: {2}", category, instance, names), ex); return(false); } }