/// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="TraceListener"/> based on an instance of <see cref="SystemDiagnosticsTraceListenerData"/>.
        /// </summary>
        /// <remarks>
        /// The building process for non Enterprise Library specific <see cref="TraceListener"/>s mimics the creation process used by System.Diagnostics' configuration.
        /// </remarks>
        /// <seealso cref="TraceListenerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="SystemDiagnosticsTraceListenerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of a <see cref="TraceListener"/> subclass.</returns>
        public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            BasicCustomTraceListenerData castedObjectConfiguration
                = (BasicCustomTraceListenerData)objectConfiguration;

            Type                type = castedObjectConfiguration.Type;
            string              name = castedObjectConfiguration.Name;
            TraceOptions        traceOutputOptions = castedObjectConfiguration.TraceOutputOptions;
            string              initData           = castedObjectConfiguration.InitData;
            NameValueCollection attributes         = castedObjectConfiguration.Attributes;

            TraceListener traceListener = null;

            if (String.IsNullOrEmpty(initData))
            {
                ConstructorInfo defaultConstructor = type.GetConstructor(Type.EmptyTypes);
                if (defaultConstructor != null)
                {
                    traceListener = (TraceListener)defaultConstructor.Invoke(emptyParameters);
                }
                else
                {
                    throw new InvalidOperationException(
                              string.Format(Resources.Culture,
                                            Resources.ExceptionCustomListenerTypeDoesNotHaveDefaultConstructor,
                                            name,
                                            type.FullName));
                }
            }
            else
            {
                try
                {
                    traceListener = (TraceListener)Activator.CreateInstance(type, initData);
                }
                catch (MissingMethodException exception)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  Resources.Culture,
                                  Resources.ExceptionCustomTraceListenerTypeDoesNotHaveRequiredConstructor,
                                  name,
                                  type.FullName),
                              exception);
                }
            }

            foreach (string attribute in attributes.Keys)
            {
                traceListener.Attributes.Add(attribute, attributes.Get(attribute));
            }

            return(traceListener);
        }
Esempio n. 2
0
        public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            BasicCustomTraceListenerData castedObjectConfiguration
                = (BasicCustomTraceListenerData)objectConfiguration;
            Type                type = castedObjectConfiguration.Type;
            string              name = castedObjectConfiguration.Name;
            TraceOptions        traceOutputOptions = castedObjectConfiguration.TraceOutputOptions;
            string              initData           = castedObjectConfiguration.InitData;
            NameValueCollection attributes         = castedObjectConfiguration.Attributes;

            return(SystemDiagnosticsTraceListenerCreationHelper.CreateSystemDiagnosticsTraceListener(name, type, initData, attributes));
        }