Fix infrequent hangs in test-runner. (#16793)
[mono-project.git] / mcs / class / referencesource / System / services / monitoring / system / diagnosticts / DiagnosticsConfigurationHandler.cs
blob9f38a265f4d81e3a3caa6a41f88aa995a8f2319c
1 //------------------------------------------------------------------------------
2 // <copyright file="DiagnosticsConfigurationHandler.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
7 #if !LIB
8 #define TRACE
9 #define DEBUG
10 namespace System.Diagnostics {
12 using System;
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Xml;
16 using System.Configuration;
17 using System.Reflection;
18 using System.Globalization;
20 /// <devdoc>
21 /// The configuration section handler for the diagnostics section of the configuration
22 /// file. The section handler participates in the resolution of configuration settings
23 /// between the &lt;diagnostics&gt; and &lt;/diagnostics&gt;portion of the .config file.
24 /// </devdoc>
25 /// <internalonly/>
26 [Obsolete("This class has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")]
27 public class DiagnosticsConfigurationHandler : IConfigurationSectionHandler {
29 /// <devdoc>
30 /// <para>Parses the configuration settings between the
31 /// &lt;diagnostics&gt; and &lt;/diagnostics&gt; portion of the .config file to populate
32 /// the values of 'WebServicesConfiguration' object and returning it.
33 /// </para>
34 /// </devdoc>
35 /// <internalonly/>
36 public virtual object Create(object parent, object configContext, XmlNode section) {
37 bool foundSwitches = false;
38 bool foundAssert = false;
39 bool foundTrace = false;
40 bool foundCounters = false;
42 HandlerBase.CheckForUnrecognizedAttributes(section);
44 // Since the tracing and switch code lives in System.Dll and config is in System.Configuration.dll
45 // the settings just go into a hashtable to communicate to the values to the diagnostics code in System.dll
46 Hashtable parentConfig = (Hashtable)parent;
47 Hashtable config;
48 if (parentConfig == null)
49 config = new Hashtable();
50 else
51 config = (Hashtable)parentConfig.Clone();
53 foreach (XmlNode child in section.ChildNodes) {
54 if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child))
55 continue;
57 switch (child.Name) {
58 case "switches":
59 if (foundSwitches)
60 throw new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionsUnique, "switches"));
61 foundSwitches = true;
63 HandleSwitches(config, child, configContext);
64 break;
65 case "assert":
66 if (foundAssert)
67 throw new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionsUnique, "assert"));
68 foundAssert = true;
70 HandleAssert(config, child, configContext);
71 break;
72 case "trace":
73 if (foundTrace)
74 throw new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionsUnique, "trace"));
75 foundTrace = true;
77 HandleTrace(config, child, configContext);
78 break;
79 case "performanceCounters":
80 if (foundCounters)
81 throw new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionsUnique, "performanceCounters"));
82 foundCounters = true;
84 HandleCounters((Hashtable)parent, config, child, configContext);
85 break;
86 default:
87 HandlerBase.ThrowUnrecognizedElement(child);
88 break;
89 } // switch(child.Name)
91 HandlerBase.CheckForUnrecognizedAttributes(child);
93 return config;
96 private static void HandleSwitches(Hashtable config, XmlNode switchesNode, object context) {
97 Hashtable switches = (Hashtable) new SwitchesDictionarySectionHandler().Create(config["switches"], context, switchesNode);
98 IDictionaryEnumerator en = switches.GetEnumerator();
99 while (en.MoveNext()) {
100 try {
101 Int32.Parse((string) en.Value, CultureInfo.InvariantCulture);
103 catch {
104 throw new ConfigurationErrorsException(SR.GetString(SR.Value_must_be_numeric, en.Key));
108 config["switches"] = switches;
111 private static void HandleAssert(Hashtable config, XmlNode assertNode, object context) {
112 bool assertuienabled = false;
113 if (HandlerBase.GetAndRemoveBooleanAttribute(assertNode, "assertuienabled", ref assertuienabled) != null)
114 config["assertuienabled"] = assertuienabled;
116 string logfilename = null;
117 if (HandlerBase.GetAndRemoveStringAttribute(assertNode, "logfilename", ref logfilename) != null)
118 config["logfilename"] = logfilename;
120 HandlerBase.CheckForChildNodes(assertNode);
123 private static void HandleCounters(Hashtable parent, Hashtable config, XmlNode countersNode, object context) {
124 int filemappingsize = 0;
125 if (HandlerBase.GetAndRemoveIntegerAttribute(countersNode, "filemappingsize", ref filemappingsize) != null) {
126 //Should only be handled at machine config level
127 if (parent == null)
128 config["filemappingsize"] = filemappingsize;
131 HandlerBase.CheckForChildNodes(countersNode);
134 private static void HandleTrace(Hashtable config, XmlNode traceNode, object context) {
135 bool foundListeners = false;
136 bool autoflush = false;
137 if (HandlerBase.GetAndRemoveBooleanAttribute(traceNode, "autoflush", ref autoflush) != null)
138 config["autoflush"] = autoflush;
140 int indentsize = 0;
141 if (HandlerBase.GetAndRemoveIntegerAttribute(traceNode, "indentsize", ref indentsize) != null)
142 config["indentsize"] = indentsize;
144 foreach (XmlNode traceChild in traceNode.ChildNodes) {
145 if (HandlerBase.IsIgnorableAlsoCheckForNonElement(traceChild))
146 continue;
148 if (traceChild.Name == "listeners") {
149 if (foundListeners)
150 throw new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionsUnique, "listeners"));
151 foundListeners = true;
153 HandleListeners(config, traceChild, context);
155 else {
156 HandlerBase.ThrowUnrecognizedElement(traceChild);
161 private static void HandleListeners(Hashtable config, XmlNode listenersNode, object context) {
162 HandlerBase.CheckForUnrecognizedAttributes(listenersNode);
163 foreach (XmlNode listenersChild in listenersNode.ChildNodes) {
164 if (HandlerBase.IsIgnorableAlsoCheckForNonElement(listenersChild))
165 continue;
167 string name = null, className = null, initializeData = null;
168 string op = listenersChild.Name;
170 switch (op) {
171 case "add":
172 case "remove":
173 case "clear":
174 break;
175 default:
176 HandlerBase.ThrowUnrecognizedElement(listenersChild);
177 break;
180 HandlerBase.GetAndRemoveStringAttribute(listenersChild, "name", ref name);
181 HandlerBase.GetAndRemoveStringAttribute(listenersChild, "type", ref className);
182 HandlerBase.GetAndRemoveStringAttribute(listenersChild, "initializeData", ref initializeData);
183 HandlerBase.CheckForUnrecognizedAttributes(listenersChild);
184 HandlerBase.CheckForChildNodes(listenersChild);
186 TraceListener newListener = null;
187 if (className != null) {
188 Type t = Type.GetType(className);
190 if (t == null)
191 throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_find_type, className));
193 if (!typeof(TraceListener).IsAssignableFrom(t))
194 throw new ConfigurationErrorsException(SR.GetString(SR.Type_isnt_tracelistener, className));
196 // create a listener with parameterless constructor
197 if (initializeData == null) {
198 ConstructorInfo ctorInfo = t.GetConstructor(new Type[] {});
199 if (ctorInfo == null)
200 throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className));
201 newListener = (TraceListener)(SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { }));
203 // create a listener with a one-string constructor
204 else {
205 ConstructorInfo ctorInfo = t.GetConstructor(new Type[] { typeof(string) });
206 if (ctorInfo == null)
207 throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_get_constructor, className));
208 newListener = (TraceListener)(SecurityUtils.ConstructorInfoInvoke(ctorInfo, new object[] { initializeData }));
210 if (name != null) {
211 newListener.Name = name;
215 // we already verified above that we only have "add", "remove", or "clear", so we can
216 // switch on the first char here for perf.
217 switch (op[0]) {
218 case 'a':
219 if (newListener == null)
220 throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_listener, name));
222 Trace.Listeners.Add(newListener);
224 break;
225 case 'r':
226 if (newListener == null) {
227 // no type specified, we'll have to delete by name
229 // if no name is specified we can't do anything
230 if (name == null)
231 throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_remove_with_null));
233 Trace.Listeners.Remove(name);
235 else {
236 // remove by listener
237 Trace.Listeners.Remove(newListener);
239 break;
240 case 'c':
241 Trace.Listeners.Clear();
242 break;
243 default:
244 HandlerBase.ThrowUnrecognizedElement(listenersChild);
245 break;
251 internal class SwitchesDictionarySectionHandler : DictionarySectionHandler {
252 protected override string KeyAttributeName {
253 get { return "name";}
256 internal override bool ValueRequired {
257 get { return true; }
263 #endif