示例#1
0
        /// <summary>
        ///     Removes the counter (category) from the system
        /// </summary>
        public static void Delete(string categoryName)
        {
            CheckValidCategory(categoryName);
            string machineName = ".";

            categoryName = categoryName.ToLower(CultureInfo.InvariantCulture);

            Mutex mutex = null;

            try
            {
                SharedUtils.EnterMutex(PerfMutexName, ref mutex);
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.Format(SR.CantDeleteCategory));
                }

                SharedPerformanceCounter.RemoveAllInstances(categoryName);

                PerformanceCounterLib.UnregisterCategory(categoryName);
                PerformanceCounterLib.CloseAllLibraries();
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        public static void Delete(string categoryName)
        {
            CheckValidCategory(categoryName);
            string machineName = ".";

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName).Demand();
            SharedUtils.CheckNtEnvironment();
            categoryName = categoryName.ToLower(CultureInfo.InvariantCulture);
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxperf.1.0", ref mutex);
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString("CantDeleteCategory"));
                }
                SharedPerformanceCounter.RemoveAllInstances(categoryName);
                PerformanceCounterLib.UnregisterCategory(categoryName);
                PerformanceCounterLib.CloseAllLibraries();
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
示例#3
0
        private static void DeleteCategory(string categoryName, string machineName)
        {
            CheckValidCategory(categoryName);

            if (machineName != "." && String.Compare(machineName, PerformanceCounterLib.ComputerName, true, CultureInfo.InvariantCulture) != 0)
            {
                throw new NotSupportedException(SR.GetString(SR.RemoteCounterAdmin));
            }

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "machineName", machineName));
            }

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = SharedUtils.EnterMutex(perfMutexName);

            try {
                if (!PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.CantDeleteCategory));
                }

                PerformanceCounterLib.UnregisterCategory(machineName, categoryName);
            }
            finally {
                mutex.ReleaseMutex();
                mutex.Close();
            }
        }
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            if (categoryType < PerformanceCounterCategoryType.Unknown || categoryType > PerformanceCounterCategoryType.MultiInstance)
            {
                throw new ArgumentOutOfRangeException("categoryType");
            }
            if (counterData == null)
            {
                throw new ArgumentNullException("counterData");
            }

            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                // null categoryHelp is a valid option - it gets set to "Help Not Available" later on.
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                SharedUtils.EnterMutex(perfMutexName, ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.PerformanceCategoryExists, categoryName));
                }

                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            PerformanceCounterCategory category;

            if ((categoryType < PerformanceCounterCategoryType.Unknown) || (categoryType > PerformanceCounterCategoryType.MultiInstance))
            {
                throw new ArgumentOutOfRangeException("categoryType");
            }
            if (counterData == null)
            {
                throw new ArgumentNullException("counterData");
            }
            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName).Demand();
            SharedUtils.CheckNtEnvironment();
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxperf.1.0", ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString("PerformanceCategoryExists", new object[] { categoryName }));
                }
                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                category = new PerformanceCounterCategory(categoryName, machineName);
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
            return(category);
        }
示例#6
0
        public static PerformanceCounterCategory Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
        {
            if (categoryType < PerformanceCounterCategoryType.Unknown || categoryType > PerformanceCounterCategoryType.MultiInstance)
            {
                throw new ArgumentOutOfRangeException(nameof(categoryType));
            }
            if (counterData == null)
            {
                throw new ArgumentNullException(nameof(counterData));
            }

            CheckValidCategory(categoryName);
            if (categoryHelp != null)
            {
                // null categoryHelp is a valid option - it gets set to "Help Not Available" later on.
                CheckValidHelp(categoryHelp);
            }
            string machineName = ".";

            Mutex mutex = null;

            try
            {
                SharedUtils.EnterMutex(PerfMutexName, ref mutex);
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.Format(SR.PerformanceCategoryExists, categoryName));
                }

                CheckValidCounterLayout(counterData);
                PerformanceCounterLib.RegisterCategory(categoryName, categoryType, categoryHelp, counterData);
                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
        public static void DeleteEventSource(string source, string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                RegistryKey key = null;
                using (key = FindSourceRegistration(source, machineName, true))
                {
                    if (key == null)
                    {
                        if (machineName == null)
                        {
                            throw new ArgumentException(SR.GetString("LocalSourceNotRegistered", new object[] { source }));
                        }
                        throw new ArgumentException(SR.GetString("SourceNotRegistered", new object[] { source, machineName, @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog" }));
                    }
                    string name = key.Name;
                    int    num  = name.LastIndexOf('\\');
                    if (string.Compare(name, num + 1, source, 0, name.Length - num, StringComparison.Ordinal) == 0)
                    {
                        throw new InvalidOperationException(SR.GetString("CannotDeleteEqualSource", new object[] { source }));
                    }
                }
                try
                {
                    key = FindSourceRegistration(source, machineName, false);
                    key.DeleteSubKeyTree(source);
                    if (!SkipRegPatch)
                    {
                        string[]  strArray = (string[])key.GetValue("Sources");
                        ArrayList list     = new ArrayList(strArray.Length - 1);
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (strArray[i] != source)
                            {
                                list.Add(strArray[i]);
                            }
                        }
                        string[] array = new string[list.Count];
                        list.CopyTo(array);
                        key.SetValue("Sources", array, RegistryValueKind.MultiString);
                    }
                }
                finally
                {
                    if (key != null)
                    {
                        key.Flush();
                        key.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                }
            }
        }
        public static void Delete(string logName, string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameterFormat", new object[] { "machineName" }));
            }
            if ((logName == null) || (logName.Length == 0))
            {
                throw new ArgumentException(SR.GetString("NoLogName"));
            }
            if (!ValidLogName(logName, false))
            {
                throw new InvalidOperationException(SR.GetString("BadLogName"));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            RegistryKey eventLogRegKey = null;
            Mutex       mutex          = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                try
                {
                    eventLogRegKey = GetEventLogRegKey(machineName, true);
                    if (eventLogRegKey == null)
                    {
                        throw new InvalidOperationException(SR.GetString("RegKeyNoAccess", new object[] { @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog", machineName }));
                    }
                    using (RegistryKey key2 = eventLogRegKey.OpenSubKey(logName))
                    {
                        if (key2 == null)
                        {
                            throw new InvalidOperationException(SR.GetString("MissingLog", new object[] { logName, machineName }));
                        }
                        EventLog log = new EventLog(logName, machineName);
                        try
                        {
                            log.Clear();
                        }
                        finally
                        {
                            log.Close();
                        }
                        string path = null;
                        try
                        {
                            path = (string)key2.GetValue("File");
                        }
                        catch
                        {
                        }
                        if (path != null)
                        {
                            try
                            {
                                File.Delete(path);
                            }
                            catch
                            {
                            }
                        }
                    }
                    eventLogRegKey.DeleteSubKeyTree(logName);
                }
                finally
                {
                    if (eventLogRegKey != null)
                    {
                        eventLogRegKey.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                }
            }
        }
        public static void CreateEventSource(EventSourceCreationData sourceData)
        {
            if (sourceData == null)
            {
                throw new ArgumentNullException("sourceData");
            }
            string logName     = sourceData.LogName;
            string source      = sourceData.Source;
            string machineName = sourceData.MachineName;

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
            }
            if ((logName == null) || (logName.Length == 0))
            {
                logName = "Application";
            }
            if (!ValidLogName(logName, false))
            {
                throw new ArgumentException(SR.GetString("BadLogName"));
            }
            if ((source == null) || (source.Length == 0))
            {
                throw new ArgumentException(SR.GetString("MissingParameter", new object[] { "source" }));
            }
            if ((source.Length + @"SYSTEM\CurrentControlSet\Services\EventLog".Length) > 0xfe)
            {
                throw new ArgumentException(SR.GetString("ParameterTooLong", new object[] { "source", 0xfe - @"SYSTEM\CurrentControlSet\Services\EventLog".Length }));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                if (SourceExists(source, machineName, true))
                {
                    if (".".Equals(machineName))
                    {
                        throw new ArgumentException(SR.GetString("LocalSourceAlreadyExists", new object[] { source }));
                    }
                    throw new ArgumentException(SR.GetString("SourceAlreadyExists", new object[] { source, machineName }));
                }
                _UnsafeGetAssertPermSet().Assert();
                RegistryKey localMachine = null;
                RegistryKey keyParent    = null;
                RegistryKey logKey       = null;
                RegistryKey sourceLogKey = null;
                RegistryKey key5         = null;
                try
                {
                    if (machineName == ".")
                    {
                        localMachine = Registry.LocalMachine;
                    }
                    else
                    {
                        localMachine = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName);
                    }
                    keyParent = localMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog", true);
                    if (keyParent == null)
                    {
                        if (!".".Equals(machineName))
                        {
                            throw new InvalidOperationException(SR.GetString("RegKeyMissing", new object[] { @"SYSTEM\CurrentControlSet\Services\EventLog", logName, source, machineName }));
                        }
                        throw new InvalidOperationException(SR.GetString("LocalRegKeyMissing", new object[] { @"SYSTEM\CurrentControlSet\Services\EventLog", logName, source }));
                    }
                    logKey = keyParent.OpenSubKey(logName, true);
                    if ((logKey == null) && (logName.Length >= 8))
                    {
                        string strA = logName.Substring(0, 8);
                        if (((string.Compare(strA, "AppEvent", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(strA, "SecEvent", StringComparison.OrdinalIgnoreCase) == 0)) || (string.Compare(strA, "SysEvent", StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            throw new ArgumentException(SR.GetString("InvalidCustomerLogName", new object[] { logName }));
                        }
                        string str5 = FindSame8FirstCharsLog(keyParent, logName);
                        if (str5 != null)
                        {
                            throw new ArgumentException(SR.GetString("DuplicateLogName", new object[] { logName, str5 }));
                        }
                    }
                    bool flag = logKey == null;
                    if (flag)
                    {
                        if (SourceExists(logName, machineName, true))
                        {
                            if (".".Equals(machineName))
                            {
                                throw new ArgumentException(SR.GetString("LocalLogAlreadyExistsAsSource", new object[] { logName }));
                            }
                            throw new ArgumentException(SR.GetString("LogAlreadyExistsAsSource", new object[] { logName, machineName }));
                        }
                        logKey = keyParent.CreateSubKey(logName);
                        if (!SkipRegPatch)
                        {
                            logKey.SetValue("Sources", new string[] { logName, source }, RegistryValueKind.MultiString);
                        }
                        SetSpecialLogRegValues(logKey, logName);
                        sourceLogKey = logKey.CreateSubKey(logName);
                        SetSpecialSourceRegValues(sourceLogKey, sourceData);
                    }
                    if (logName != source)
                    {
                        if (!flag)
                        {
                            SetSpecialLogRegValues(logKey, logName);
                            if (!SkipRegPatch)
                            {
                                string[] array = logKey.GetValue("Sources") as string[];
                                if (array == null)
                                {
                                    logKey.SetValue("Sources", new string[] { logName, source }, RegistryValueKind.MultiString);
                                }
                                else if (Array.IndexOf <string>(array, source) == -1)
                                {
                                    string[] destinationArray = new string[array.Length + 1];
                                    Array.Copy(array, destinationArray, array.Length);
                                    destinationArray[array.Length] = source;
                                    logKey.SetValue("Sources", destinationArray, RegistryValueKind.MultiString);
                                }
                            }
                        }
                        key5 = logKey.CreateSubKey(source);
                        SetSpecialSourceRegValues(key5, sourceData);
                    }
                }
                finally
                {
                    if (localMachine != null)
                    {
                        localMachine.Close();
                    }
                    if (keyParent != null)
                    {
                        keyParent.Close();
                    }
                    if (logKey != null)
                    {
                        logKey.Flush();
                        logKey.Close();
                    }
                    if (sourceLogKey != null)
                    {
                        sourceLogKey.Flush();
                        sourceLogKey.Close();
                    }
                    if (key5 != null)
                    {
                        key5.Flush();
                        key5.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex.Close();
                }
            }
        }
示例#10
0
        internal static PerformanceCounterCategory Create(string categoryName, string categoryHelp, CounterCreationDataCollection counterData, string machineName, string localizedIniFilePath)
        {
            CheckValidCategory(categoryName);

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "machineName", machineName));
            }

            if (machineName != "." && String.Compare(machineName, PerformanceCounterLib.ComputerName, true, CultureInfo.InvariantCulture) != 0)
            {
                throw new NotSupportedException(SR.GetString(SR.RemoteCounterAdmin));
            }

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);

            permission.Demand();

            SharedUtils.CheckNtEnvironment();

            Mutex mutex = SharedUtils.EnterMutex(perfMutexName);

            try {
                if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName) || PerformanceCounterLib.CategoryExists(machineName, categoryName))
                {
                    throw new InvalidOperationException(SR.GetString(SR.PerformanceCategoryExists));
                }

                // Ensure that there are no duplicate counter names being created
                Hashtable h = new Hashtable();
                for (int i = 0; i < counterData.Count; i++)
                {
                    // Ensure that all counter help strings aren't null or empty
                    if (counterData[i].CounterName == null || counterData[i].CounterName.Length == 0)
                    {
                        throw new ArgumentException(SR.GetString(SR.InvalidCounterName));
                    }

                    int currentSampleType = (int)counterData[i].CounterType;
                    if ((currentSampleType == NativeMethods.PERF_AVERAGE_BULK) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_QUEUELEN_TYPE) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE) ||
                        (currentSampleType == NativeMethods.PERF_100NSEC_MULTI_TIMER) ||
                        (currentSampleType == NativeMethods.PERF_100NSEC_MULTI_TIMER_INV) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_MULTI_TIMER) ||
                        (currentSampleType == NativeMethods.PERF_COUNTER_MULTI_TIMER_INV) ||
                        (currentSampleType == NativeMethods.PERF_RAW_FRACTION) ||
                        (currentSampleType == NativeMethods.PERF_SAMPLE_FRACTION) ||
                        (currentSampleType == NativeMethods.PERF_SAMPLE_COUNTER) ||
                        (currentSampleType == NativeMethods.PERF_AVERAGE_TIMER))
                    {
                        if (counterData.Count <= (i + 1))
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                        }
                        else
                        {
                            currentSampleType = (int)counterData[i + 1].CounterType;


                            if (currentSampleType != NativeMethods.PERF_AVERAGE_BASE &&
                                currentSampleType != NativeMethods.PERF_COUNTER_MULTI_BASE &&
                                currentSampleType != NativeMethods.PERF_RAW_BASE &&
                                currentSampleType != NativeMethods.PERF_SAMPLE_BASE)
                            {
                                throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                            }
                        }
                    }
                    else if (currentSampleType == NativeMethods.PERF_AVERAGE_BASE ||
                             currentSampleType == NativeMethods.PERF_COUNTER_MULTI_BASE ||
                             currentSampleType == NativeMethods.PERF_RAW_BASE ||
                             currentSampleType == NativeMethods.PERF_SAMPLE_BASE)
                    {
                        if (i == 0)
                        {
                            throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                        }
                        else
                        {
                            currentSampleType = (int)counterData[i - 1].CounterType;

                            if (
                                (currentSampleType != NativeMethods.PERF_AVERAGE_BULK) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_QUEUELEN_TYPE) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_LARGE_QUEUELEN_TYPE) &&
                                (currentSampleType != NativeMethods.PERF_100NSEC_MULTI_TIMER) &&
                                (currentSampleType != NativeMethods.PERF_100NSEC_MULTI_TIMER_INV) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_MULTI_TIMER) &&
                                (currentSampleType != NativeMethods.PERF_COUNTER_MULTI_TIMER_INV) &&
                                (currentSampleType != NativeMethods.PERF_RAW_FRACTION) &&
                                (currentSampleType != NativeMethods.PERF_SAMPLE_FRACTION) &&
                                (currentSampleType != NativeMethods.PERF_SAMPLE_COUNTER) &&
                                (currentSampleType != NativeMethods.PERF_AVERAGE_TIMER))
                            {
                                throw new InvalidOperationException(SR.GetString(SR.CounterLayout));
                            }
                        }
                    }

                    if (h.ContainsKey(counterData[i].CounterName))
                    {
                        throw new ArgumentException(SR.GetString(SR.DuplicateCounterName, counterData[i].CounterName));
                    }
                    else
                    {
                        h.Add(counterData[i].CounterName, String.Empty);

                        // Ensure that all counter help strings aren't null or empty
                        if (counterData[i].CounterHelp == null || counterData[i].CounterHelp.Length == 0)
                        {
                            counterData[i].CounterHelp = counterData[i].CounterName + " help";
                        }
                    }
                }

                if (localizedIniFilePath == null)
                {
                    PerformanceCounterLib.RegisterCategory(machineName, categoryName, categoryHelp, counterData);
                }
                else
                {
                    PerformanceCounterLib.RegisterCategory(machineName, categoryName, categoryHelp, counterData, localizedIniFilePath);
                }

                return(new PerformanceCounterCategory(categoryName, machineName));
            }
            finally {
                mutex.ReleaseMutex();
                mutex.Close();
            }
        }