Пример #1
0
        /// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.Install"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            Context.LogMessage(Res.GetString(Res.CreatingPerformanceCounter, CategoryName));

            RegistryKey categoryKey = null;
            RegistryKey subKey      = null;
            RegistryKey servicesKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services", true);

            stateSaver["categoryKeyExisted"] = false;

            // save the old values of the Performance and Linkage subkeys in case we need to rollback the install
            try {
                if (servicesKey != null)
                {
                    categoryKey = servicesKey.OpenSubKey(CategoryName, true);
                    if (categoryKey != null)
                    {
                        stateSaver["categoryKeyExisted"] = true;

                        subKey = categoryKey.OpenSubKey("Performance");
                        if (subKey != null)
                        {
                            stateSaver["performanceKeyData"] = new SerializableRegistryKey(subKey);
                            subKey.Close();
                            categoryKey.DeleteSubKeyTree("Performance");
                        }

                        subKey = categoryKey.OpenSubKey("Linkage");
                        if (subKey != null)
                        {
                            stateSaver["linkageKeyData"] = new SerializableRegistryKey(subKey);
                            subKey.Close();
                            categoryKey.DeleteSubKeyTree("Linkage");
                        }
                    }
                }
            }
            finally {
                if (categoryKey != null)
                {
                    categoryKey.Close();
                }
                if (servicesKey != null)
                {
                    servicesKey.Close();
                }
            }

            // delete the "Performance" and "Linkage" subkeys, unregister the category,
            // and finally re-create the category from scratch.
            if (PerformanceCounterCategory.Exists(CategoryName))
            {
                PerformanceCounterCategory.Delete(CategoryName);
            }
            PerformanceCounterCategory.Create(CategoryName, CategoryHelp, categoryType, Counters);
        }
 public override void Uninstall(IDictionary savedState)
 {
     base.Uninstall(savedState);
     if (this.UninstallAction == System.Configuration.Install.UninstallAction.Remove)
     {
         base.Context.LogMessage(Res.GetString("RemovingPerformanceCounter", new object[] { this.CategoryName }));
         PerformanceCounterCategory.Delete(this.CategoryName);
     }
 }
Пример #3
0
        /// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.Uninstall"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

            if (UninstallAction == UninstallAction.Remove)
            {
                Context.LogMessage(Res.GetString(Res.RemovingPerformanceCounter, CategoryName));
                PerformanceCounterCategory.Delete(CategoryName);
            }
        }
        /// <summary>
        /// Installs the specified performance <paramref name="counters" />.
        /// </summary>
        /// <param name="counters">A list of counters to install.</param>
        /// <exception cref="ArgumentNullException"><paramref name="counters" /> is <c>null</c>.</exception>
        public static void Install(this IEnumerable <PerformanceCounterSettings> counters)
        {
            if (counters == null)
            {
                throw new ArgumentNullException(nameof(counters));
            }

            // Group counters by category
            var countersByCategories =
                (
                    from
                    counter in counters
                    group
                    counter by counter.Category
                    into
                    countersByCategory
                    select
                    countersByCategory
                ).ToDictionary(c => c.Key);

            // For each counters by category
            foreach (var countersByCategory in countersByCategories)
            {
                // Get group category
                var category = countersByCategory.Key;

                // Check if category exists
                if (PerformanceCounterCategory.Exists(category))
                {
                    // Check if all counters exists within the category
                    if (countersByCategory.Value.All(counter => PerformanceCounterCategory.CounterExists(counter.Name, counter.Category)))
                    {
                        continue;
                    }

                    // Some or all counters does not exists within the category
                    // Delete category
                    PerformanceCounterCategory.Delete(category);
                }

                // Create new counters collection
                var counterData = new CounterCreationDataCollection();

                foreach (var counterCreationData in countersByCategory.Value.Select(counterConfig => new CounterCreationData(counterConfig.Name, String.Empty, counterConfig.CounterType)))
                {
                    counterData.Add(counterCreationData);
                }

                // Create category with counters
                PerformanceCounterCategory.Create(category, String.Empty, PerformanceCounterCategoryType.SingleInstance, counterData);
            }
        }
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            base.Context.LogMessage(Res.GetString("CreatingPerformanceCounter", new object[] { this.CategoryName }));
            RegistryKey key       = null;
            RegistryKey keyToSave = null;
            RegistryKey key3      = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services", true);

            stateSaver["categoryKeyExisted"] = false;
            try
            {
                if (key3 != null)
                {
                    key = key3.OpenSubKey(this.CategoryName, true);
                    if (key != null)
                    {
                        stateSaver["categoryKeyExisted"] = true;
                        keyToSave = key.OpenSubKey("Performance");
                        if (keyToSave != null)
                        {
                            stateSaver["performanceKeyData"] = new SerializableRegistryKey(keyToSave);
                            keyToSave.Close();
                            key.DeleteSubKeyTree("Performance");
                        }
                        keyToSave = key.OpenSubKey("Linkage");
                        if (keyToSave != null)
                        {
                            stateSaver["linkageKeyData"] = new SerializableRegistryKey(keyToSave);
                            keyToSave.Close();
                            key.DeleteSubKeyTree("Linkage");
                        }
                    }
                }
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
                if (key3 != null)
                {
                    key3.Close();
                }
            }
            if (PerformanceCounterCategory.Exists(this.CategoryName))
            {
                PerformanceCounterCategory.Delete(this.CategoryName);
            }
            PerformanceCounterCategory.Create(this.CategoryName, this.CategoryHelp, this.categoryType, this.Counters);
        }