mscorlib(4.0.0.0) API with additions
TraceSwitch.cs
1 #define TRACE
3 
4 namespace System.Diagnostics
5 {
7  [SwitchLevel(typeof(TraceLevel))]
8  public class TraceSwitch : Switch
9  {
14  public TraceLevel Level
15  {
16  get
17  {
18  return (TraceLevel)base.SwitchSetting;
19  }
20  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
21  set
22  {
23  if (value < TraceLevel.Off || value > TraceLevel.Verbose)
24  {
25  throw new ArgumentException(SR.GetString("TraceSwitchInvalidLevel"));
26  }
27  base.SwitchSetting = (int)value;
28  }
29  }
30 
34  public bool TraceError => Level >= TraceLevel.Error;
35 
39  public bool TraceWarning => Level >= TraceLevel.Warning;
40 
44  public bool TraceInfo => Level >= TraceLevel.Info;
45 
49  public bool TraceVerbose => Level == TraceLevel.Verbose;
50 
54  public TraceSwitch(string displayName, string description)
55  : base(displayName, description)
56  {
57  }
58 
63  public TraceSwitch(string displayName, string description, string defaultSwitchValue)
64  : base(displayName, description, defaultSwitchValue)
65  {
66  }
67 
69  protected override void OnSwitchSettingChanged()
70  {
71  int switchSetting = base.SwitchSetting;
72  if (switchSetting < 0)
73  {
74  Trace.WriteLine(SR.GetString("TraceSwitchLevelTooLow", base.DisplayName));
75  base.SwitchSetting = 0;
76  }
77  else if (switchSetting > 4)
78  {
79  Trace.WriteLine(SR.GetString("TraceSwitchLevelTooHigh", base.DisplayName));
80  base.SwitchSetting = 4;
81  }
82  }
83 
85  protected override void OnValueChanged()
86  {
87  base.SwitchSetting = (int)Enum.Parse(typeof(TraceLevel), base.Value, ignoreCase: true);
88  }
89  }
90 }
static void WriteLine(string message)
Writes a message to the trace listeners in the P:System.Diagnostics.Trace.Listeners collection.
Definition: Trace.cs:259
Describes a set of security permissions applied to code. This class cannot be inherited.
TraceSwitch(string displayName, string description, string defaultSwitchValue)
Initializes a new instance of the T:System.Diagnostics.TraceSwitch class, using the specified display...
Definition: TraceSwitch.cs:63
Provides a set of methods and properties that help you trace the execution of your code....
Definition: Trace.cs:6
Definition: __Canon.cs:3
bool TraceError
Gets a value indicating whether the switch allows error-handling messages.
Definition: TraceSwitch.cs:34
TraceLevel Level
Gets or sets the trace level that determines the messages the switch allows.
Definition: TraceSwitch.cs:15
TraceLevel
Specifies what messages to output for the T:System.Diagnostics.Debug, T:System.Diagnostics....
Definition: TraceLevel.cs:4
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
bool TraceVerbose
Gets a value indicating whether the switch allows all messages.
Definition: TraceSwitch.cs:49
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides the base class for enumerations.
Definition: Enum.cs:14
override void OnSwitchSettingChanged()
Updates and corrects the level for this switch.
Definition: TraceSwitch.cs:69
override void OnValueChanged()
Sets the P:System.Diagnostics.Switch.SwitchSetting property to the integer equivalent of the P:System...
Definition: TraceSwitch.cs:85
Provides a multilevel switch to control tracing and debug output without recompiling your code.
Definition: TraceSwitch.cs:8
The exception that is thrown when one of the arguments provided to a method is not valid.
bool TraceWarning
Gets a value indicating whether the switch allows warning messages.
Definition: TraceSwitch.cs:39
TraceSwitch(string displayName, string description)
Initializes a new instance of the T:System.Diagnostics.TraceSwitch class, using the specified display...
Definition: TraceSwitch.cs:54
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides an abstract base class to create new debugging and tracing switches.
Definition: Switch.cs:10
bool TraceInfo
Gets a value indicating whether the switch allows informational messages.
Definition: TraceSwitch.cs:44