private void AddTraceListener (IDictionary d, XElement child, IDictionary<string,XAttribute> attributes, TraceListenerCollection listeners)
			{
			string name = GetAttribute (attributes, "name", true, child);
			string type = null;

#if CONFIGURATION_DEP
			type = GetAttribute (attributes, "type", false, child);
			if (type == null)
				{
				// indicated by name.
				TraceListener shared = GetSharedListeners (d)[name];
				if (shared == null)
					throw new ConfigurationException (String.Format ("Shared trace listener {0} does not exist.", name));
				if (child.HasAttributes)
					throw new ConfigurationErrorsException (
						string.Format ("Listener '{0}' references a shared " +
						"listener and can only have a 'Name' " +
						"attribute.", name));
				listeners.Add (shared, configValues);
				return;
				}
#else
			type = GetAttribute (attributes, "type", true, child);
#endif

#if SSHARP
			CType t = Type.GetType (type);
#else
			Type t = Type.GetType (type);
#endif
			if (t == null)
				throw new ConfigurationException (string.Format ("Invalid Type Specified: {0}", type));

			object[] args;
#if SSHARP
			CType[] types;
#else
			Type[] types;
#endif

			string initializeData = GetAttribute (attributes, "initializeData", false, child);
			if (initializeData != null)
				{
				args = new object[] { initializeData };
#if SSHARP
				types = new CType[] { typeof (string) };
#else
				types = new Type[] { typeof (string) };
#endif
				}
			else
				{
				args = null;
#if NETCF
#if SSHARP
				types = new CType[0];
#else
				types = new Type[0];
#endif
#else
				types = Type.EmptyTypes;
#endif
				}
#if SSHARP
			var ctor = t.GetConstructor (types);
#else
			BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
			if (t.Assembly == GetType ().Assembly)
				flags |= BindingFlags.NonPublic;

			ConstructorInfo ctor = t.GetConstructor (flags, null, types, null);
#endif
			if (ctor == null)
				throw new ConfigurationException ("Couldn't find constructor for class " + type);

			TraceListener l = (TraceListener)ctor.Invoke (args);
			l.Name = name;

#if CONFIGURATION_DEP
			string trace = GetAttribute (attributes, "traceOutputOptions", false, child);
			if (trace != null)
				{
				if (trace != trace.Trim ())
					throw new ConfigurationErrorsException (string.Format (
"Invalid value '{0}' for 'traceOutputOptions'.",
						trace), child);

				TraceOptions trace_options;

				try
					{
					trace_options = (TraceOptions)Enum.Parse (
						typeof (TraceOptions), trace, false);
					}
				catch (ArgumentException)
					{
					throw new ConfigurationErrorsException (string.Format (
"Invalid value '{0}' for 'traceOutputOptions'.",
						trace), child);
					}

				l.TraceOutputOptions = trace_options;
				}

			string[] supported_attributes = l.GetSupportedAttributes ();
			if (supported_attributes != null)
				{
				for (int i = 0; i < supported_attributes.Length; i++)
					{
					string key = supported_attributes[i];
					string value = GetAttribute (attributes, key, false, child);
					if (value != null)
						l.Attributes.Add (key, value);
					}
				}
#endif

			listeners.Add (l, configValues);
			}
 public void AddRange(TraceListenerCollection value);
		// only defines "add" and "remove", but "clear" also works
		// for add, "name" is required; initializeData is optional; "type" is required in 1.x, optional in 2.0.
		private void AddTraceListeners (IDictionary d, XElement listenersNode, TraceListenerCollection listeners)
			{
#if !TARGET_JVM
			// There are no attributes on <listeners/>
			ValidateInvalidAttributes (listenersNode.Attributes ().ToDictionary (a=> a.Name), listenersNode);

			foreach (XElement child in listenersNode.Elements ())
				{
				XmlNodeType t = child.NodeType;
				if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
					continue;
				if (t == XmlNodeType.Element)
					{
					IDictionary<string, XAttribute> attributes = child.Attributes ().ToDictionary (a => a.Name);
					string name = null;
					switch (child.Name)
						{
						case "add":
							AddTraceListener (d, child, attributes, listeners);
							break;
						case "remove":
							name = GetAttribute (attributes, "name", true, child);
							RemoveTraceListener (name);
							break;
						case "clear":
							configValues.Listeners.Clear ();
							break;
						default:
							ThrowUnrecognizedElement (child);
							break;
						}
					ValidateInvalidAttributes (attributes, child);
					}
				else
					ThrowUnrecognizedNode (child);
				}
#endif
			}
		private TraceListenerCollection GetSharedListeners (IDictionary d)
			{
			TraceListenerCollection shared_listeners = d["sharedListeners"] as TraceListenerCollection;
			if (shared_listeners == null)
				{
				shared_listeners = new TraceListenerCollection (false);
				d["sharedListeners"] = shared_listeners;
				}
			return shared_listeners;
			}
Ejemplo n.º 5
0
 static Output()
 {
     Listeners = typeof(TraceListenerCollection)
                 .GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null)
                 .Invoke(null) as TraceListenerCollection;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Adds the contents of another
 /// <see cref="System.Diagnostics.TraceListenerCollection">TraceListenerCollection</see>
 /// to the list.
 /// </summary>
 /// <param name="listeners">
 /// Another
 /// <see cref="System.Diagnostics.TraceListenerCollection">TraceListenerCollection</see>
 /// whose contents are added to the list.
 /// </param>
 public void AddRange(TraceListenerCollection listeners)
 {
     _listeners.AddRange(listeners);
 }
 public static SuspendTrackerDisposable AssertSuspend(this TraceListenerCollection traceListenerCollection) =>
 new SuspendTrackerDisposable(traceListenerCollection);