/// <summary>
        /// Removes the performance counter data specified in the arguments
        /// </summary>
        /// <param name="data">Data to remove performance counters</param>
        /// <returns>Installation result</returns>
        public static PmcInstallationResult RemovePerformanceCounters(PmcCreationData data)
        {
            var errors     = new Dictionary <string, Exception>();
            var categories = new List <string>();

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            // --- Remove categories one-by-one
            foreach (var category in data.Categories.Values.Where(category => !category.IsPredefined))
            {
                var categoryName = AppConfigurationManager.ProviderSettings.InstancePrefix + category.Name;
                try
                {
                    // --- Delete the whole category
                    if (PerformanceCounterCategory.Exists(categoryName))
                    {
                        PerformanceCounterCategory.Delete(categoryName);
                    }
                    categories.Add(categoryName);
                }
                catch (Exception ex)
                {
                    // --- Administer errors
                    errors[categoryName] = ex;
                }
            }
            return(new PmcInstallationResult(categories, errors));
        }
Exemple #2
0
        static void Main()
        {
            // --- Create performance counters if not installed yet
            var counter = PmcManager.GetCounter<TasksProcessedPmc>("consumer");
            if (!counter.HasInstance)
            {
                Console.WriteLine("Installing performance counters...");
                var pmcData = new PmcCreationData();
                pmcData.MergeCountersFromAssembly(typeof(TasksProcessedPmc).Assembly);
                PmcManager.InstallPerformanceCounters(pmcData);
            }

            // --- Clean up the message queues
            var requestQueue = ResourceConnectionFactory.CreateResourceConnection<INamedQueue>(REQUEST_QUEUE);
            requestQueue.Clear();
            var responseQueue = ResourceConnectionFactory.CreateResourceConnection<INamedQueue>(RESPONSE_QUEUE);
            responseQueue.Clear();

            // --- Set up the background host
            var settings = AppConfigurationManager.GetSettings<BackgroundTaskHostSettings>(BACKGROUND_TASK_HOST_SECTION);
            var host = new BackgroundTaskHost(settings);

            // --- Do the work for 10 seconds
            Console.WriteLine("Starting background task host.");
            host.Start();
            Thread.Sleep(5000);

            var context = host.Configuration.DefaultContext;
            var processors = host.Configuration.GetTaskProcessors();
            foreach (var processor in processors.Where(processor => processor.Name == "Processor"))
            {
                processor.InstanceCount = 3;
                break;
            }

            host.Reconfigure(new BackgroundTaskHostSettings(context, processors));

            Thread.Sleep(5000);

            Console.WriteLine("Stopping background task host.");
            host.Stop();
        }