1 //------------------------------------------------------------------------------
2 // <copyright file="DiagnosticsConfigurationHandler.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
10 namespace System
.Diagnostics
{
13 using System
.Collections
;
14 using System
.Diagnostics
;
16 using System
.Configuration
;
17 using System
.Reflection
;
18 using System
.Globalization
;
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 <diagnostics> and </diagnostics>portion of the .config file.
26 [Obsolete("This class has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")]
27 public class DiagnosticsConfigurationHandler
: IConfigurationSectionHandler
{
30 /// <para>Parses the configuration settings between the
31 /// <diagnostics> and </diagnostics> portion of the .config file to populate
32 /// the values of 'WebServicesConfiguration' object and returning it.
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
;
48 if (parentConfig
== null)
49 config
= new Hashtable();
51 config
= (Hashtable
)parentConfig
.Clone();
53 foreach (XmlNode child
in section
.ChildNodes
) {
54 if (HandlerBase
.IsIgnorableAlsoCheckForNonElement(child
))
60 throw new ConfigurationErrorsException(SR
.GetString(SR
.ConfigSectionsUnique
, "switches"));
63 HandleSwitches(config
, child
, configContext
);
67 throw new ConfigurationErrorsException(SR
.GetString(SR
.ConfigSectionsUnique
, "assert"));
70 HandleAssert(config
, child
, configContext
);
74 throw new ConfigurationErrorsException(SR
.GetString(SR
.ConfigSectionsUnique
, "trace"));
77 HandleTrace(config
, child
, configContext
);
79 case "performanceCounters":
81 throw new ConfigurationErrorsException(SR
.GetString(SR
.ConfigSectionsUnique
, "performanceCounters"));
84 HandleCounters((Hashtable
)parent
, config
, child
, configContext
);
87 HandlerBase
.ThrowUnrecognizedElement(child
);
89 } // switch(child.Name)
91 HandlerBase
.CheckForUnrecognizedAttributes(child
);
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()) {
101 Int32
.Parse((string) en
.Value
, CultureInfo
.InvariantCulture
);
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
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
;
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
))
148 if (traceChild
.Name
== "listeners") {
150 throw new ConfigurationErrorsException(SR
.GetString(SR
.ConfigSectionsUnique
, "listeners"));
151 foundListeners
= true;
153 HandleListeners(config
, traceChild
, context
);
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
))
167 string name
= null, className
= null, initializeData
= null;
168 string op
= listenersChild
.Name
;
176 HandlerBase
.ThrowUnrecognizedElement(listenersChild
);
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
);
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
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 }
));
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.
219 if (newListener
== null)
220 throw new ConfigurationErrorsException(SR
.GetString(SR
.Could_not_create_listener
, name
));
222 Trace
.Listeners
.Add(newListener
);
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
231 throw new ConfigurationErrorsException(SR
.GetString(SR
.Cannot_remove_with_null
));
233 Trace
.Listeners
.Remove(name
);
236 // remove by listener
237 Trace
.Listeners
.Remove(newListener
);
241 Trace
.Listeners
.Clear();
244 HandlerBase
.ThrowUnrecognizedElement(listenersChild
);
251 internal class SwitchesDictionarySectionHandler
: DictionarySectionHandler
{
252 protected override string KeyAttributeName
{
253 get { return "name";}
256 internal override bool ValueRequired
{