private static void SaveMetaDataSection(ChoPerformanceCounter pc)
        {
            string            xPath        = GetXPath(pc);
            ChoPCMetaDataInfo metaDataInfo = pc.MetaDataInfo;

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(MetaDataFilepath, false, false))
            {
                if (metaDataInfo == null)
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(xPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(xPath);
                }

                if (node != null)
                {
                    ChoXmlDocument.SetOuterXml(node, metaDataInfo.ToXml());
                    xmlDocument.Save();
                }
            }
        }
        public static ChoPCMetaDataInfo GetMetaDataSection(ChoPerformanceCounter pc)
        {
            if (pc == null)
            {
                return(null);
            }

            string xPath = GetXPath(pc);

            XmlNode node = null;

            if (_rootNode != null)
            {
                node = _rootNode.SelectSingleNode(xPath);
            }

            if (node != null)
            {
                XmlAttributeOverrides overrides = new XmlAttributeOverrides();
                XmlAttributes         attr      = new XmlAttributes();
                attr.XmlRoot = new XmlRootAttribute(node.Name);
                overrides.Add(typeof(ChoPCMetaDataInfo), attr);

                ChoPCMetaDataInfo metaDataInfo = node.ToObject(typeof(ChoPCMetaDataInfo), overrides) as ChoPCMetaDataInfo;
                return(metaDataInfo);
            }
            else
            {
                return(null);
            }
        }
 private static string GetXPath(ChoPerformanceCounter pc)
 {
     if (pc.InstanceName.IsNullOrEmpty())
     {
         return("//PerformanceCounterCategory[@name='{0}']/PerformanceCounter[@name='{1}']".FormatString(pc.CategoryName, pc.CounterName));
     }
     else
     {
         return("//PerformanceCounterCategory[@name='{0}']/PerformanceCounter[@name='{1}' and @instanceName='{2}']".FormatString(pc.CategoryName, pc.CounterName, pc.InstanceName));
     }
 }
        public static bool IsMetaDataModified(ChoPerformanceCounter pc)
        {
            if (pc == null)
            {
                return(false);
            }

            ChoPCMetaDataInfo newConfigurationMetaDataInfo = GetMetaDataSection(pc);

            return(!ChoObject.Equals <ChoPCMetaDataInfo>(newConfigurationMetaDataInfo, pc.MetaDataInfo));
        }
        public static void SetMetaDataSection(ChoPerformanceCounter pc)
        {
            if (pc == null)
            {
                return;
            }

            if (ChoObject.Equals <ChoPCMetaDataInfo>(pc.MetaDataInfo, GetMetaDataSection(pc)))
            {
                return;
            }

            SaveMetaDataSection(pc);
        }
        public static ChoPCMetaDataInfo GetMetaDataSectionNSaveIfEmpty(ChoPerformanceCounter pc)
        {
            ChoPCMetaDataInfo metaData = GetMetaDataSection(pc);

            if (metaData == null)
            {
                SaveMetaDataSection(pc);
                return(pc.MetaDataInfo);
            }
            else
            {
                return(metaData);
            }
        }
        public static void SetWatcher(ChoPerformanceCounter pc)
        {
            if (pc == null)
            {
                return;
            }

            Initialize();

            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_PCMetaData_WatcherHandler".FormatString("{0}/{1}/{2}".FormatString(pc.CategoryName, pc.CounterName, pc.InstanceName)),
                                                                           (sender, e) =>
            {
                if (ChoPCMetaDataManager.IsMetaDataModified(pc))
                {
                    pc.RefreshMetaData(GetMetaDataSectionNSaveIfEmpty(pc));
                }
            });
            pc.RefreshMetaData(GetMetaDataSection(pc));
        }