private static void HandleListeners(Hashtable config, XmlNode listenersNode, object context) {
HandlerBase.CheckForUnrecognizedAttributes(listenersNode);
foreach (XmlNode listenersChild in listenersNode.ChildNodes) {
if (HandlerBase.IsIgnorableAlsoCheckForNonElement(listenersChild))
continue;
string name = null, className = null, initializeData = null;
string op = listenersChild.Name;
switch (op) {
case "add":
case "remove":
case "clear":
break;
default:
HandlerBase.ThrowUnrecognizedElement(listenersChild);
break;
}
HandlerBase.GetAndRemoveStringAttribute(listenersChild, "name", ref name);
HandlerBase.GetAndRemoveStringAttribute(listenersChild, "type", ref className);
HandlerBase.GetAndRemoveStringAttribute(listenersChild, "initializeData", ref initializeData);
HandlerBase.CheckForUnrecognizedAttributes(listenersChild);
HandlerBase.CheckForChildNodes(listenersChild);
TraceListener newListener = null;
if (className != null) {
Type t = Type.GetType(className);
if (t == null)
throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_find_type, className));
if (!typeof(TraceListener).IsAssignableFrom(t))
throw new ConfigurationErrorsException(SR.GetString(SR.Type_isnt_tracelistener, className));
// create a listener with parameterless constructor
if (initializeData == null) {
ConstructorInfo ctorInfo = t.GetConstructor(new Type[] {});
if (ctorInfo == null)
throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className));
newListener = (TraceListener)(ctorInfo.Invoke(new object[] {}));
}
// create a listener with a one-string constructor
else {
ConstructorInfo ctorInfo = t.GetConstructor(new Type[] { typeof(string) });
if (ctorInfo == null)
throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className));
newListener = (TraceListener)(ctorInfo.Invoke(new object[] { initializeData }));
}
if (name != null) {
newListener.Name = name;
}
}
// we already verified above that we only have "add", "remove", or "clear", so we can
// switch on the first char here for perf.
switch (op[0]) {
case 'a':
if (newListener == null)
throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_listener, name));
Trace.Listeners.Add(newListener);
break;
case 'r':
if (newListener == null) {
// no type specified, we'll have to delete by name
// if no name is specified we can't do anything
if (name == null)
throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_remove_with_null));
Trace.Listeners.Remove(name);
}
else {
// remove by listener
Trace.Listeners.Remove(newListener);
}
break;
case 'c':
Trace.Listeners.Clear();
break;
default:
HandlerBase.ThrowUnrecognizedElement(listenersChild);
break;
}
}
}
}