Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationItemFactory"/> class.
        /// </summary>
        /// <param name="assemblies">The assemblies to scan for named items.</param>
        public ConfigurationItemFactory(params Assembly[] assemblies)
        {
            this.CreateInstance    = FactoryHelper.CreateInstance;
            this.targets           = new Factory <Target, TargetAttribute>(this);
            this.filters           = new Factory <Filter, FilterAttribute>(this);
            this.layoutRenderers   = new LayoutRendererFactory(this);
            this.layouts           = new Factory <Layout, LayoutAttribute>(this);
            this.conditionMethods  = new MethodFactory <ConditionMethodsAttribute, ConditionMethodAttribute>();
            this.ambientProperties = new Factory <LayoutRenderer, AmbientPropertyAttribute>(this);
            this.timeSources       = new Factory <TimeSource, TimeSourceAttribute>(this);
            this.allFactories      = new List <object>
            {
                this.targets,
                this.filters,
                this.layoutRenderers,
                this.layouts,
                this.conditionMethods,
                this.ambientProperties,
                this.timeSources,
            };

            foreach (var asm in assemblies)
            {
                this.RegisterItemsFromAssembly(asm);
            }
        }
        internal ConfigurationItemFactory(ServiceRepository serviceRepository, ConfigurationItemFactory globalDefaultFactory, params Assembly[] assemblies)
        {
            _serviceRepository = serviceRepository ?? throw new ArgumentNullException(nameof(serviceRepository));
            _targets           = new Factory <Target, TargetAttribute>(serviceRepository, globalDefaultFactory?._targets);
            _filters           = new Factory <Filter, FilterAttribute>(serviceRepository, globalDefaultFactory?._filters);
            _layoutRenderers   = new LayoutRendererFactory(serviceRepository, globalDefaultFactory?._layoutRenderers);
            _layouts           = new Factory <Layout, LayoutAttribute>(serviceRepository, globalDefaultFactory?._layouts);
            _conditionMethods  = new MethodFactory(globalDefaultFactory?._conditionMethods, classType => MethodFactory.ExtractClassMethods <ConditionMethodsAttribute, ConditionMethodAttribute>(classType));
            _ambientProperties = new Factory <LayoutRenderer, AmbientPropertyAttribute>(serviceRepository, globalDefaultFactory?._ambientProperties);
            _timeSources       = new Factory <TimeSource, TimeSourceAttribute>(serviceRepository, globalDefaultFactory?._timeSources);
            _allFactories      = new IFactory[]
            {
                _targets,
                _filters,
                _layoutRenderers,
                _layouts,
                _conditionMethods,
                _ambientProperties,
                _timeSources,
            };

            foreach (var asm in assemblies)
            {
                RegisterItemsFromAssembly(asm);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationItemFactory"/> class.
        /// </summary>
        /// <param name="assemblies">The assemblies to scan for named items.</param>
        public ConfigurationItemFactory(params Assembly[] assemblies)
        {
            CreateInstance     = FactoryHelper.CreateInstance;
            _targets           = new Factory <Target, TargetAttribute>(this);
            _filters           = new Factory <Filter, FilterAttribute>(this);
            _layoutRenderers   = new LayoutRendererFactory(this);
            _layouts           = new Factory <Layout, LayoutAttribute>(this);
            _conditionMethods  = new MethodFactory(classType => MethodFactory.ExtractClassMethods <ConditionMethodsAttribute, ConditionMethodAttribute>(classType));
            _ambientProperties = new Factory <LayoutRenderer, AmbientPropertyAttribute>(this);
            _timeSources       = new Factory <TimeSource, TimeSourceAttribute>(this);
            _allFactories      = new IFactory[]
            {
                _targets,
                _filters,
                _layoutRenderers,
                _layouts,
                _conditionMethods,
                _ambientProperties,
                _timeSources,
            };

            foreach (var asm in assemblies)
            {
                RegisterItemsFromAssembly(asm);
            }
        }
        private void AddExtensionsFromElement(XmlElement element, string baseDirectory)
        {
            if (element == null)
            {
                return;
            }

            foreach (XmlElement targetElement in PropertyHelper.GetChildElements(element))
            {
                if (EqualsCI(targetElement.LocalName, "add"))
                {
                    string assemblyFile = GetCaseInsensitiveAttribute(targetElement, "assemblyFile");
                    string extPrefix    = GetCaseInsensitiveAttribute(targetElement, "prefix");
                    string prefix;
                    if (extPrefix != null && extPrefix.Length != 0)
                    {
                        prefix = extPrefix + ".";
                    }
                    else
                    {
                        prefix = String.Empty;
                    }

                    if (assemblyFile != null && assemblyFile.Length > 0)
                    {
                        try
                        {
                            string fullFileName = Path.Combine(baseDirectory, assemblyFile);
                            InternalLogger.Info("Loading assemblyFile: {0}", fullFileName);
                            Assembly asm = Assembly.LoadFrom(fullFileName);

                            TargetFactory.AddTargetsFromAssembly(asm, prefix);
                            LayoutRendererFactory.AddLayoutRenderersFromAssembly(asm, prefix);
                            FilterFactory.AddFiltersFromAssembly(asm, prefix);
                            LayoutFactory.AddLayoutsFromAssembly(asm, prefix);
                            ConditionMethodFactory.AddConditionMethodsFromAssembly(asm, prefix);
                        }
                        catch (Exception ex)
                        {
                            InternalLogger.Error("Error loading extensions: {0}", ex);
                            if (LogManager.ThrowExceptions)
                            {
                                throw new NLogConfigurationException("Error loading extensions: " + assemblyFile, ex);
                            }
                        }
                        continue;
                    }
                    ;

                    string assemblyName = GetCaseInsensitiveAttribute(targetElement, "assembly");

                    if (assemblyName != null && assemblyName.Length > 0)
                    {
                        try
                        {
                            InternalLogger.Info("Loading assemblyName: {0}", assemblyName);
                            Assembly asm = Assembly.Load(assemblyName);

                            TargetFactory.AddTargetsFromAssembly(asm, prefix);
                            LayoutRendererFactory.AddLayoutRenderersFromAssembly(asm, prefix);
                            FilterFactory.AddFiltersFromAssembly(asm, prefix);
                            LayoutFactory.AddLayoutsFromAssembly(asm, prefix);
                            ConditionMethodFactory.AddConditionMethodsFromAssembly(asm, prefix);
                        }
                        catch (Exception ex)
                        {
                            InternalLogger.Error("Error loading extensions: {0}", ex);
                            if (LogManager.ThrowExceptions)
                            {
                                throw new NLogConfigurationException("Error loading extensions: " + assemblyName, ex);
                            }
                        }
                        continue;
                    }
                    ;
                }
            }
        }
Beispiel #5
0
 public LayoutRendererFactory(ServiceRepository serviceRepository, LayoutRendererFactory globalDefaultFactory) : base(serviceRepository, globalDefaultFactory)
 {
     _globalDefaultFactory = globalDefaultFactory;
 }