zoukankan      html  css  js  c++  java
  • PerformanceCountersHelper

    
    namespace ConsoleApplication
    {
        using System;
        using System.Linq;
        using System.Collections.Generic;
        using System.Reflection;
        using Microshaoft;
        /// <summary>
        /// Class1 的摘要说明。
        /// </summary>
        public class PerformanceCountersCategoryCreater
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            //[STAThread]
            static void Main(string[] args)
            {
                string assemblyPath = args[0];
                string typeName = args[1];
                string category = args[2];
                var assembly = Assembly.LoadFile(assemblyPath);
                var typesList = assembly.GetTypes().Where
                                                    (
                                                        (x) =>
                                                        {
                                                            return (x.Name.ToLower().IndexOf(typeName.ToLower()) >= 0);
                                                        }
                                                    ).ToList();
                typesList.ForEach
                            (
                                (x) =>
                                {
                                    PerformanceCountersHelper.CreatePerformanceCountersCategory
                                                                        (
                                                                            category
                                                                            , x
                                                                        );
                                }
                            );
                Console.WriteLine("Hello World");
                Console.WriteLine(Environment.Version.ToString());
                Console.ReadLine();
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        using System.Linq;
        using System.Diagnostics;
        using System.Reflection;
        using System.Collections.Generic;
        //interface IPerformanceCountersManager
        //{
        //    public string PerformanceCountersInstanceName { set; get; }
        //    public string PerformanceCountersCategoryName { set; get; }
        //}
        public static class PerformanceCountersHelper
        {
            public static void CreatePerformanceCountersCategory
                                        (
                                            string performanceCountersCategoryName
                                            , Type type
                                        )
            {
                //var type = typeof(T);
                var propertiesList = type.GetProperties().ToList();
                propertiesList = propertiesList.Where
                                                    (
                                                        (pi) =>
                                                        {
                                                            return (pi.PropertyType == typeof(PerformanceCounter));
                                                        }
                                                    ).ToList();
                if (PerformanceCounterCategory.Exists(performanceCountersCategoryName))
                {
                    propertiesList.ForEach
                                        (
                                            (pi) =>
                                            {
                                                if (PerformanceCounterCategory.CounterExists(pi.Name, performanceCountersCategoryName))
                                                {
                                                    //if (PerformanceCounterCategory.InstanceExists(performanceCountersInstanceName, category))
                                                    //{
                                                    //    //var pc = new PerformanceCounter(category, pi.Name, instanceName, false);
                                                    //    //pc.InstanceName = instanceName;
                                                    //    //pc.RemoveInstance();
                                                    //}
                                                }
                                            }
                                        );
                    PerformanceCounterCategory.Delete(performanceCountersCategoryName);
                }
                var ccdc = new CounterCreationDataCollection();
                propertiesList.ForEach
                                (
                                    (pi) =>
                                    {
                                        var propertyName = pi.Name;
                                        var ccd = PerformanceCounterHelper.GetCounterCreationData
                                                                                (
                                                                                    propertyName
                                                                                    , PerformanceCounterType.NumberOfItems64
                                                                                );
                                        ccdc.Add(ccd);
                                    }
                                );
                PerformanceCounterCategory.Create
                                (
                                    performanceCountersCategoryName,
                                    string.Format("{0} Category Help.", performanceCountersCategoryName),
                                    PerformanceCounterCategoryType.MultiInstance,
                                    ccdc
                                );
            }
            public static void AttachPerformanceCountersToProperties<T>
                                        (
                                            string performanceCounterInstanceName
                                            , string category
                                            , T target = default(T)
                                        )
            {
                var type = typeof(T);
                var propertiesList = type.GetProperties().ToList();
                propertiesList = propertiesList.Where
                                                    (
                                                        (pi) =>
                                                        {
                                                            return (pi.PropertyType == typeof(PerformanceCounter));
                                                        }
                                                    ).ToList();
                propertiesList.ForEach
                                (
                                    (pi) =>
                                    {
                                        var propertyName = pi.Name;
                                        var pc = new PerformanceCounter()
                                        {
                                            CategoryName = category
                                            , CounterName = propertyName
                                            , InstanceLifetime = PerformanceCounterInstanceLifetime.Process
                                            , InstanceName = performanceCounterInstanceName
                                            , ReadOnly = false
                                            , RawValue = 0
                                        };
                                        if (pi.GetGetMethod().IsStatic)
                                        {
                                            var setter = DynamicPropertyAccessor.CreateSetStaticPropertyValueAction<PerformanceCounter>(type, propertyName);
                                            setter(pc);
                                        }
                                        else
                                        {
                                            if (target != null)
                                            {
                                                var setter = DynamicPropertyAccessor.CreateSetPropertyValueAction<PerformanceCounter>(type, propertyName);
                                                setter(target, pc);
                                            }
                                        }
                                    }
                                );
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        using System.Diagnostics;
        public static class PerformanceCounterHelper
        {
            public static CounterCreationData GetCounterCreationData(string counterName, PerformanceCounterType performanceCounterType)
            {
                return new CounterCreationData()
                {
                    CounterName = counterName
                    , CounterHelp = string.Format("{0} Help", counterName)
                    , CounterType = performanceCounterType
                };
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        using System.Reflection;
        using System.Linq;
        using System.Linq.Expressions;
        public class DynamicPropertyAccessor
        {
            private static Assembly GetAssemblyByTypeName(string typeName)
            {
                return AppDomain.CurrentDomain.GetAssemblies().First
                                                            (
                                                                (a) =>
                                                                    {
                                                                        return a.GetTypes().Any
                                                                                            (
                                                                                                (t) =>
                                                                                                    {
                                                                                                        return (t.FullName == typeName);
                                                                                                    }
                                                                                            );
                                                                    }
                                                            );
            }
            public static Func<object, object> CreateGetPropertyValueFunc(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateGetPropertyValueFunc(type, propertyName);
            }
            public static Func<object, object> CreateGetPropertyValueFunc(Type type, string propertyName)
            {
                var target = Expression.Parameter(typeof(object));
                var castTarget = Expression.Convert(target, type);
                var getPropertyValue = Expression.Property(castTarget, propertyName);
                var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
                var lambda = Expression.Lambda<Func<object, object>>(castPropertyValue, target);
                return lambda.Compile();
            }
            public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateGetPropertyValueFunc<TProperty>(type, propertyName);
            }
            public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>(Type type, string propertyName)
            {
                var target = Expression.Parameter(typeof(object));
                var castTarget = Expression.Convert(target, type);
                var getPropertyValue = Expression.Property(castTarget, propertyName);
                var lambda = Expression.Lambda<Func<object, TProperty>>(getPropertyValue, target);
                return lambda.Compile();
            }
            public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateGetStaticPropertyValueFunc<TProperty>(type, propertyName);
            }
            public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName, typeof(TProperty));
                var getPropertyValue = Expression.Property(null, property);
                var lambda = Expression.Lambda<Func<TProperty>>(getPropertyValue, null);
                return lambda.Compile();
            }
            public static Func<object> CreateGetStaticPropertyValueFunc(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName);
                var getPropertyValue = Expression.Property(null, property);
                var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
                var lambda = Expression.Lambda<Func<object>>(castPropertyValue, null);
                return lambda.Compile();
            }
            public static Func<object> CreateGetStaticPropertyValueFunc(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateGetStaticPropertyValueFunc(type, propertyName);
            }
            public static Action<object, object> CreateSetPropertyValueAction(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName);
                var target = Expression.Parameter(typeof(object));
                var propertyValue = Expression.Parameter(typeof(object));
                var castTarget = Expression.Convert(target, type);
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object, object>>(call, target, propertyValue);
                return lambda.Compile();
            }
            public static Action<object, object> CreateSetPropertyValueAction(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateSetPropertyValueAction(type, propertyName);
            }
            public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName);
                var target = Expression.Parameter(typeof(object));
                var propertyValue = Expression.Parameter(typeof(TProperty));
                var castTarget = Expression.Convert(target, type);
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object, TProperty>>(call, target, propertyValue);
                return lambda.Compile();
            }
            public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateSetPropertyValueAction<TProperty>(type, propertyName);
            }
            public static Action<object> CreateSetStaticPropertyValueAction(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName);
                var propertyValue = Expression.Parameter(typeof(object));
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(null, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object>>(call, propertyValue);
                return lambda.Compile();
            }
            public static Action<object> CreateSetStaticPropertyValueAction(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateSetStaticPropertyValueAction(type, propertyName);
            }
            public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>(Type type, string propertyName)
            {
                var property = type.GetProperty(propertyName);
                var propertyValue = Expression.Parameter(typeof(TProperty));
                //var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(null, getSetMethod, propertyValue);
                var lambda = Expression.Lambda<Action<TProperty>>(call, propertyValue);
                return lambda.Compile();
            }
            public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
            {
                Type type;
                if (isTypeFromAssembly)
                {
                    var assembly = GetAssemblyByTypeName(typeName);
                    type = assembly.GetType(typeName);
                }
                else
                {
                    type = Type.GetType(typeName);
                }
                return CreateSetStaticPropertyValueAction<TProperty>(type, propertyName);
            }
        }
    }
    
    
  • 相关阅读:
    使用MobaXterm远程连接Ubuntu,启动Octave,界面不能正常显示
    ABP .Net Core 日志组件集成使用NLog
    ABP .Net Core Entity Framework迁移使用MySql数据库
    ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
    阿里云 Angular 2 UI框架 NG-ZORRO介绍
    Visual Studio 2019 Window Form 本地打包发布猫腻
    VS Code + NWJS(Node-Webkit)0.14.7 + SQLite3 + Angular6 构建跨平台桌面应用
    ABP .Net Core 调用异步方法抛异常A second operation started on this context before a previous asynchronous operation completed
    ABP .Net Core To Json序列化配置
    .Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2483582.html
Copyright © 2011-2022 走看看