mscorlib(4.0.0.0) API with additions
Switch.cs
6 
7 namespace System.Diagnostics
8 {
10  public abstract class Switch
11  {
12  private SwitchElementsCollection switchSettings;
13 
14  private readonly string description;
15 
16  private readonly string displayName;
17 
18  private int switchSetting;
19 
20  private volatile bool initialized;
21 
22  private bool initializing;
23 
24  private volatile string switchValueString = string.Empty;
25 
26  private StringDictionary attributes;
27 
28  private string defaultValue;
29 
30  private static List<WeakReference> switches = new List<WeakReference>();
31 
32  private static int s_LastCollectionCount;
33 
36  [XmlIgnore]
38  {
39  get
40  {
41  Initialize();
42  if (attributes == null)
43  {
44  attributes = new StringDictionary();
45  }
46  return attributes;
47  }
48  }
49 
52  public string DisplayName => displayName;
53 
56  public string Description
57  {
58  get
59  {
60  if (description != null)
61  {
62  return description;
63  }
64  return string.Empty;
65  }
66  }
67 
70  protected int SwitchSetting
71  {
72  get
73  {
74  if (!initialized && InitializeWithStatus())
75  {
77  }
78  return switchSetting;
79  }
80  set
81  {
82  bool flag = false;
83  lock (TraceInternal.critSec)
84  {
85  initialized = true;
86  if (switchSetting != value)
87  {
88  switchSetting = value;
89  flag = true;
90  }
91  }
92  if (flag)
93  {
95  }
96  }
97  }
98 
102  protected string Value
103  {
104  get
105  {
106  Initialize();
107  return switchValueString;
108  }
109  set
110  {
111  Initialize();
112  switchValueString = value;
113  try
114  {
115  OnValueChanged();
116  }
117  catch (ArgumentException inner)
118  {
119  throw new ConfigurationErrorsException(SR.GetString("BadConfigSwitchValue", DisplayName), inner);
120  }
121  catch (FormatException inner2)
122  {
123  throw new ConfigurationErrorsException(SR.GetString("BadConfigSwitchValue", DisplayName), inner2);
124  }
125  catch (OverflowException inner3)
126  {
127  throw new ConfigurationErrorsException(SR.GetString("BadConfigSwitchValue", DisplayName), inner3);
128  }
129  }
130  }
131 
135  protected Switch(string displayName, string description)
136  : this(displayName, description, "0")
137  {
138  }
139 
144  protected Switch(string displayName, string description, string defaultSwitchValue)
145  {
146  if (displayName == null)
147  {
148  displayName = string.Empty;
149  }
150  this.displayName = displayName;
151  this.description = description;
152  lock (switches)
153  {
154  _pruneCachedSwitches();
155  switches.Add(new WeakReference(this));
156  }
157  defaultValue = defaultSwitchValue;
158  }
159 
160  private static void _pruneCachedSwitches()
161  {
162  lock (switches)
163  {
164  if (s_LastCollectionCount != GC.CollectionCount(2))
165  {
166  List<WeakReference> list = new List<WeakReference>(switches.Count);
167  for (int i = 0; i < switches.Count; i++)
168  {
169  Switch @switch = (Switch)switches[i].Target;
170  if (@switch != null)
171  {
172  list.Add(switches[i]);
173  }
174  }
175  if (list.Count < switches.Count)
176  {
177  switches.Clear();
178  switches.AddRange(list);
179  switches.TrimExcess();
180  }
181  s_LastCollectionCount = GC.CollectionCount(2);
182  }
183  }
184  }
185 
186  private void Initialize()
187  {
188  InitializeWithStatus();
189  }
190 
191  private bool InitializeWithStatus()
192  {
193  if (!initialized)
194  {
195  lock (TraceInternal.critSec)
196  {
197  if (initialized || initializing)
198  {
199  return false;
200  }
201  initializing = true;
202  if (switchSettings == null && !InitializeConfigSettings())
203  {
204  initialized = true;
205  initializing = false;
206  return false;
207  }
208  if (switchSettings != null)
209  {
210  SwitchElement switchElement = switchSettings[displayName];
211  if (switchElement != null)
212  {
213  string value = switchElement.Value;
214  if (value != null)
215  {
216  Value = value;
217  }
218  else
219  {
220  Value = defaultValue;
221  }
222  try
223  {
224  TraceUtils.VerifyAttributes(switchElement.Attributes, GetSupportedAttributes(), this);
225  }
226  catch (ConfigurationException)
227  {
228  initialized = false;
229  initializing = false;
230  throw;
231  }
232  attributes = new StringDictionary();
233  attributes.ReplaceHashtable(switchElement.Attributes);
234  }
235  else
236  {
237  switchValueString = defaultValue;
238  OnValueChanged();
239  }
240  }
241  else
242  {
243  switchValueString = defaultValue;
244  OnValueChanged();
245  }
246  initialized = true;
247  initializing = false;
248  }
249  }
250  return true;
251  }
252 
253  private bool InitializeConfigSettings()
254  {
255  if (switchSettings != null)
256  {
257  return true;
258  }
259  if (!DiagnosticsConfiguration.CanInitialize())
260  {
261  return false;
262  }
263  switchSettings = DiagnosticsConfiguration.SwitchSettings;
264  return true;
265  }
266 
269  protected internal virtual string[] GetSupportedAttributes()
270  {
271  return null;
272  }
273 
275  protected virtual void OnSwitchSettingChanged()
276  {
277  }
278 
280  protected virtual void OnValueChanged()
281  {
283  }
284 
285  internal static void RefreshAll()
286  {
287  lock (switches)
288  {
289  _pruneCachedSwitches();
290  for (int i = 0; i < switches.Count; i++)
291  {
292  ((Switch)switches[i].Target)?.Refresh();
293  }
294  }
295  }
296 
297  internal void Refresh()
298  {
299  lock (TraceInternal.critSec)
300  {
301  initialized = false;
302  switchSettings = null;
303  Initialize();
304  }
305  }
306  }
307 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
virtual void OnValueChanged()
Invoked when the P:System.Diagnostics.Switch.Value property is changed.
Definition: Switch.cs:280
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
virtual internal string [] GetSupportedAttributes()
Gets the custom attributes supported by the switch.
Definition: Switch.cs:269
Switch(string displayName, string description, string defaultSwitchValue)
Initializes a new instance of the T:System.Diagnostics.Switch class, specifying the display name,...
Definition: Switch.cs:144
Implements a hash table with the key and the value strongly typed to be strings rather than objects.
Definition: __Canon.cs:3
The exception that is thrown when an arithmetic, casting, or conversion operation in a checked contex...
Represents a weak reference, which references an object while still allowing that object to be reclai...
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
Switch(string displayName, string description)
Initializes a new instance of the T:System.Diagnostics.Switch class.
Definition: Switch.cs:135
void AddRange(IEnumerable< T > collection)
Adds the elements of the specified collection to the end of the T:System.Collections....
Definition: List.cs:545
string Value
Gets or sets the value of the switch.
Definition: Switch.cs:103
The exception that is thrown when a configuration system error has occurred.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual void OnSwitchSettingChanged()
Invoked when the P:System.Diagnostics.Switch.SwitchSetting property is changed.
Definition: Switch.cs:275
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Definition: List.cs:614
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
int SwitchSetting
Gets or sets the current setting for this switch.
Definition: Switch.cs:71
static int CollectionCount(int generation)
Returns the number of times garbage collection has occurred for the specified generation of objects.
Definition: GC.cs:242
void TrimExcess()
Sets the capacity to the actual number of elements in the T:System.Collections.Generic....
Definition: List.cs:1540
StringDictionary Attributes
Gets the custom switch attributes defined in the application configuration file.
Definition: Switch.cs:38
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Provides an abstract base class to create new debugging and tracing switches.
Definition: Switch.cs:10
string DisplayName
Gets a name used to identify the switch.
Definition: Switch.cs:52
string Description
Gets a description of the switch.
Definition: Switch.cs:57