Beispiel #1
0
        private static object CreateObject(string assemblyQualifiedTypeName, Predicate <Type> typePredicate = null)
        {
            var foundType = Type.GetType(assemblyQualifiedTypeName);

            if (foundType == null)
            {
                WriteDebugLine(
                    String.Format("Type NOT FOUND forassembly qualified name='{0}'", assemblyQualifiedTypeName),
                    LoggingConfiguration.GetDebugCategory("LoggingInfrastructure", "CreateObject")
                    );
                return(null);
            }

            if (typePredicate != null && typePredicate(foundType) == false)
            {
                return(null);
            }

            try
            {
                return(Activator.CreateInstance(foundType));
            }
            catch (Exception exc)
            {
                WriteDebugLine(
                    exc.ToString(),
                    LoggingConfiguration.GetDebugCategory("LoggingInfrastructure", "CreateObject")
                    );
                return(null);
            }
        }
Beispiel #2
0
        public static ILogAdapter CreateAdapter(LogAdapterElement configuration)
        {
            if (configuration == null)
            {
                return(null);
            }

            var adapterTypeString = configuration.Type;

            ILogAdapter result;
            var         predefinedType = PredefinedLogAdapters
                                         .FirstOrDefault(kvp => String.Compare(kvp.Key, adapterTypeString, StringComparison.InvariantCultureIgnoreCase) == 0)
                                         .Value;

            if (predefinedType != null)
            {
                try
                {
                    result = Activator.CreateInstance(predefinedType) as ILogAdapter;
                }
                catch (Exception exc)
                {
                    WriteDebugLine(
                        String.Format(
                            "Could not create instance of predefined type {0}: {1}",
                            PredefinedLogAdapters[adapterTypeString].Name,
                            exc
                            ),
                        LoggingConfiguration.GetDebugCategory(typeof(LoggingInfrastructure).Name)
                        );
                    return(null);
                }
            }
            else
            {
                result = CreateObject(
                    configuration.Type,
                    type => type.IsAbstract == false && type.IsPublic && type.GetInterface(typeof(ILogAdapter).Name) != null
                    ) as ILogAdapter;
            }

            if (result == null)
            {
                return(null);
            }



            #region setting simple ILogAdapter props

            result.Name                = configuration.Name;
            result.IsEnabled           = configuration.IsEnabled;
            result.MessageFormatString = string.IsNullOrEmpty(configuration.MessageFormatString)
                ? LogMessageFormatString.Default
                : new LogMessageFormatString(configuration.MessageFormatString);
            #endregion

            #region IPacketLogAdapter stuff

            var resultWithPackageLogging = result as IPacketLogAdapter;
            if (resultWithPackageLogging != null)
            {
                if (configuration.PackSize > 0)
                {
                    resultWithPackageLogging.PackSize = configuration.PackSize;
                }

                if (configuration.SendPeriod > 0)
                {
                    resultWithPackageLogging.SendPeriod = configuration.SendPeriod;
                }
            }
            #endregion

            if (configuration.Initialization != null && configuration.Initialization.InitializationXml != null)
            {
                result.Initialize(configuration.Initialization.InitializationXml.OuterXml);
            }

            return(result);
        }