The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ TraceSwitch

  1. //------------------------------------------------------------------------------
  2. // <copyright file="TraceSwitch.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.Diagnostics
  18. {
  19.     using System;
  20.     using System.ComponentModel;
  21.     using System.Security;
  22.     using System.Security.Permissions;
  23.    
  24.     /// <devdoc>
  25.     /// <para>Provides a multi-level switch to enable or disable tracing
  26.     /// and debug output for a compiled application or framework.</para>
  27.     /// </devdoc>
  28.     [SwitchLevel(typeof(TraceLevel))]
  29.     public class TraceSwitch : Switch
  30.     {
  31.        
  32.         /// <devdoc>
  33.         /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TraceSwitch'/> class.</para>
  34.         /// </devdoc>
  35.         public TraceSwitch(string displayName, string description) : base(displayName, description)
  36.         {
  37.         }
  38.        
  39.         public TraceSwitch(string displayName, string description, string defaultSwitchValue) : base(displayName, description, defaultSwitchValue)
  40.         {
  41.         }
  42.        
  43.         /// <devdoc>
  44.         /// <para>Gets or sets the trace
  45.         /// level that specifies what messages to output for tracing and debugging.</para>
  46.         /// </devdoc>
  47.         public TraceLevel Level {
  48.             get { return (TraceLevel)SwitchSetting; }
  49.            
  50.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  51.             set {
  52.                 if (value < TraceLevel.Off || value > TraceLevel.Verbose)
  53.                     throw new ArgumentException(SR.GetString(SR.TraceSwitchInvalidLevel));
  54.                 SwitchSetting = (int)value;
  55.             }
  56.         }
  57.        
  58.         /// <devdoc>
  59.         /// <para>Gets a value
  60.         /// indicating whether the <see cref='System.Diagnostics.TraceSwitch.Level'/> is set to
  61.         /// <see langword='Error'/>, <see langword='Warning'/>, <see langword='Info'/>, or
  62.         /// <see langword='Verbose'/>.</para>
  63.         /// </devdoc>
  64.         public bool TraceError {
  65.             get { return (Level >= TraceLevel.Error); }
  66.         }
  67.        
  68.         /// <devdoc>
  69.         /// <para>Gets a value
  70.         /// indicating whether the <see cref='System.Diagnostics.TraceSwitch.Level'/> is set to
  71.         /// <see langword='Warning'/>, <see langword='Info'/>, or <see langword='Verbose'/>.</para>
  72.         /// </devdoc>
  73.         public bool TraceWarning {
  74.             get { return (Level >= TraceLevel.Warning); }
  75.         }
  76.        
  77.         /// <devdoc>
  78.         /// <para>Gets a value
  79.         /// indicating whether the <see cref='System.Diagnostics.TraceSwitch.Level'/> is set to
  80.         /// <see langword='Info'/> or <see langword='Verbose'/>.</para>
  81.         /// </devdoc>
  82.         public bool TraceInfo {
  83.             get { return (Level >= TraceLevel.Info); }
  84.         }
  85.        
  86.         /// <devdoc>
  87.         /// <para>Gets a value
  88.         /// indicating whether the <see cref='System.Diagnostics.TraceSwitch.Level'/> is set to
  89.         /// <see langword='Verbose'/>.</para>
  90.         /// </devdoc>
  91.         public bool TraceVerbose {
  92.             get { return (Level == TraceLevel.Verbose); }
  93.         }
  94.        
  95.         /// <devdoc>
  96.         /// <para>
  97.         /// Update the level for this switch.
  98.         /// </para>
  99.         /// </devdoc>
  100.         protected override void OnSwitchSettingChanged()
  101.         {
  102.             int level = SwitchSetting;
  103.             if (level < (int)TraceLevel.Off) {
  104.                 Trace.WriteLine(SR.GetString(SR.TraceSwitchLevelTooLow, DisplayName));
  105.                 SwitchSetting = (int)TraceLevel.Off;
  106.             }
  107.             else if (level > (int)TraceLevel.Verbose) {
  108.                 Trace.WriteLine(SR.GetString(SR.TraceSwitchLevelTooHigh, DisplayName));
  109.                 SwitchSetting = (int)TraceLevel.Verbose;
  110.             }
  111.         }
  112.        
  113.         protected override void OnValueChanged()
  114.         {
  115.             SwitchSetting = (int)Enum.Parse(typeof(TraceLevel), Value, true);
  116.         }
  117.     }
  118. }

Developer Fusion