Пример #1
0
        private static void InitializeConfigurationFromConfig()
        {
            var configSection = ConfigurationHelper.LoadPureProfilerConfigurationSection();//  ConfigurationManager.GetSection("pureprofiler");

            if (configSection != null && !(configSection is PureProfilerConfigurationSection))
            {
                throw new PureProfilerException("Invalid configuration, check the 'pureprofiler' configuration section.");
            }
            if (configSection == null)
            {
                return;
            }

            var ProfilerConfig = (configSection as PureProfilerConfigurationSection);
            var providerType   = Type.GetType(ProfilerConfig.Provider, true);
            var provider       = (IConfigurationProvider)Activator.CreateInstance(providerType);

            Configuration    = provider;
            ProfilingStorage = provider.Storage;
            if (provider.Filters != null)
            {
                foreach (var filter in provider.Filters)
                {
                    ProfilingFilters.Add(filter);
                }
            }
            CircularBuffer = provider.CircularBuffer;
        }
Пример #2
0
        private static void InitializeConfigurationFromConfig()
        {
            var config = ConfigurationHelper.GetConfiguration();

            if (config == null)
            {
                return;
            }

            var logProviderName = config.GetValue <string>("logProvider");

            if (!string.IsNullOrEmpty(logProviderName))
            {
                var logProviderType = Type.GetType(logProviderName, true);
                if (logProviderType != null)
                {
                    ILoggerProvider logProvider;
                    if (logProviderType.GetConstructor(new Type[0]) == null)
                    {
                        logProvider = Activator.CreateInstance(logProviderType, new object[] { null }) as ILoggerProvider;
                    }
                    else
                    {
                        logProvider = Activator.CreateInstance(logProviderType) as ILoggerProvider;
                    }

                    if (logProvider == null)
                    {
                        throw new InvalidOperationException("Invalid log provider: " + logProviderName);
                    }

                    LoggerFactory.AddProvider(logProvider);
                }
            }

            var providerName = config.GetValue <string>("provider");

            if (string.IsNullOrEmpty(providerName))
            {
                // load configuration from config directly

                // load storage
                var storageName = config.GetValue <string>("storage");
                if (!string.IsNullOrEmpty(storageName))
                {
                    var type = Type.GetType(storageName, true);
                    ProfilingStorage = Activator.CreateInstance(type) as IProfilingStorage;
                }

                // load CircularBuffer size
                var circularBufferSizeStr = config.GetValue <string>("circularBufferSize");
                if (!string.IsNullOrEmpty(circularBufferSizeStr))
                {
                    var circularBufferSize = int.Parse(circularBufferSizeStr);
                    CircularBuffer = new CircularBuffer <ITimingSession>(circularBufferSize);
                }

                // load filters
                var filtersSection = config.GetSection("filters");
                if (filtersSection != null)
                {
                    var filters = new List <FilterConfigurationItem>();
                    filtersSection.Bind(filters);

                    foreach (var filter in filters)
                    {
                        if (string.IsNullOrWhiteSpace(filter.Type) ||
                            string.Equals(filter.Type, "contain", StringComparison.OrdinalIgnoreCase))
                        {
                            ProfilingFilters.Add(new NameContainsProfilingFilter(filter.Value));
                        }
                        else if (string.Equals(filter.Type, "regex", StringComparison.OrdinalIgnoreCase))
                        {
                            ProfilingFilters.Add(new RegexProfilingFilter(new Regex(filter.Value, RegexOptions.Compiled | RegexOptions.IgnoreCase)));
                        }
                        else if (string.Equals(filter.Type, "disable", StringComparison.OrdinalIgnoreCase))
                        {
                            ProfilingFilters.Add(new DisableProfilingFilter());
                        }
                        else
                        {
                            var filterType = Type.GetType(filter.Type, true);
                            if (!typeof(IProfilingFilter).IsAssignableFrom(filterType))
                            {
                                throw new Exception("Invalid type name: " + filter.Type);
                            }

                            try
                            {
                                ProfilingFilters.Add((IProfilingFilter)Activator.CreateInstance(filterType, new object[] { filter.Value }));
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("Invalid type name: " + filter.Type, ex);
                            }
                        }
                    }
                }

                return;
            }

            // load configuration from provider
            var providerType = Type.GetType(providerName, true);
            var provider     = (Configuration.IConfigurationProvider)Activator.CreateInstance(providerType);

            if (provider.Storage != null)
            {
                ProfilingStorage = provider.Storage;
            }

            if (provider.Filters != null)
            {
                foreach (var filter in provider.Filters)
                {
                    ProfilingFilters.Add(filter);
                }
            }

            if (provider.CircularBuffer != null)
            {
                CircularBuffer = provider.CircularBuffer;
            }
        }