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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DiagnosticsConfiguration.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. namespace System.Diagnostics
  16. {
  17.     using System;
  18.     using System.Reflection;
  19.     using System.Collections;
  20.     using System.Configuration;
  21.     using System.Threading;
  22.     using System.Runtime.Versioning;
  23.    
  24.     internal enum InitState
  25.     {
  26.         NotInitialized,
  27.         Initializing,
  28.         Initialized
  29.     }
  30.    
  31.     static internal class DiagnosticsConfiguration
  32.     {
  33.         private static SystemDiagnosticsSection configSection;
  34.         private static InitState initState = InitState.NotInitialized;
  35.        
  36.         // setting for Switch.switchSetting
  37.         static internal SwitchElementsCollection SwitchSettings {
  38.             get {
  39.                 Initialize();
  40.                 SystemDiagnosticsSection configSectionSav = configSection;
  41.                 if (configSectionSav != null)
  42.                     return configSectionSav.Switches;
  43.                 else
  44.                     return null;
  45.             }
  46.         }
  47.        
  48.         // setting for DefaultTraceListener.AssertUIEnabled
  49.         static internal bool AssertUIEnabled {
  50.             get {
  51.                 Initialize();
  52.                 SystemDiagnosticsSection configSectionSav = configSection;
  53.                 if (configSectionSav != null && configSectionSav.Assert != null)
  54.                     return configSectionSav.Assert.AssertUIEnabled;
  55.                 else
  56.                     return true;
  57.                 // the default
  58.             }
  59.         }
  60.        
  61.         static internal string ConfigFilePath {
  62.             get {
  63.                 Initialize();
  64.                 SystemDiagnosticsSection configSectionSav = configSection;
  65.                 if (configSectionSav != null)
  66.                     return configSectionSav.ElementInformation.Source;
  67.                 else
  68.                     return string.Empty;
  69.                 // the default
  70.             }
  71.         }
  72.        
  73.         // setting for DefaultTraceListener.LogFileName
  74.         static internal string LogFileName {
  75.             [ResourceExposure(ResourceScope.Machine)]
  76.             [ResourceConsumption(ResourceScope.Machine)]
  77.             get {
  78.                 Initialize();
  79.                 SystemDiagnosticsSection configSectionSav = configSection;
  80.                 if (configSectionSav != null && configSectionSav.Assert != null)
  81.                     return configSectionSav.Assert.LogFileName;
  82.                 else
  83.                     return string.Empty;
  84.                 // the default
  85.             }
  86.         }
  87.        
  88.         // setting for TraceInternal.AutoFlush
  89.         static internal bool AutoFlush {
  90.             get {
  91.                 Initialize();
  92.                 SystemDiagnosticsSection configSectionSav = configSection;
  93.                 if (configSectionSav != null && configSectionSav.Trace != null)
  94.                     return configSectionSav.Trace.AutoFlush;
  95.                 else
  96.                     return false;
  97.                 // the default
  98.             }
  99.         }
  100.        
  101.         // setting for TraceInternal.UseGlobalLock
  102.         static internal bool UseGlobalLock {
  103.             get {
  104.                 Initialize();
  105.                 SystemDiagnosticsSection configSectionSav = configSection;
  106.                 if (configSectionSav != null && configSectionSav.Trace != null)
  107.                     return configSectionSav.Trace.UseGlobalLock;
  108.                 else
  109.                     return true;
  110.                 // the default
  111.             }
  112.         }
  113.        
  114.         // setting for TraceInternal.IndentSize
  115.         static internal int IndentSize {
  116.             get {
  117.                 Initialize();
  118.                 SystemDiagnosticsSection configSectionSav = configSection;
  119.                 if (configSectionSav != null && configSectionSav.Trace != null)
  120.                     return configSectionSav.Trace.IndentSize;
  121.                 else
  122.                     return 4;
  123.                 // the default
  124.             }
  125.         }
  126.        
  127.        
  128.         static internal ListenerElementsCollection SharedListeners {
  129.             get {
  130.                 Initialize();
  131.                 SystemDiagnosticsSection configSectionSav = configSection;
  132.                 if (configSectionSav != null)
  133.                     return configSectionSav.SharedListeners;
  134.                 else
  135.                     return null;
  136.             }
  137.         }
  138.        
  139.         static internal SourceElementsCollection Sources {
  140.             get {
  141.                 Initialize();
  142.                 SystemDiagnosticsSection configSectionSav = configSection;
  143.                 if (configSectionSav != null && configSectionSav.Sources != null)
  144.                     return configSectionSav.Sources;
  145.                 else
  146.                     return null;
  147.             }
  148.         }
  149.        
  150.         static internal SystemDiagnosticsSection SystemDiagnosticsSection {
  151.             get {
  152.                 Initialize();
  153.                 return configSection;
  154.             }
  155.         }
  156.        
  157.         private static SystemDiagnosticsSection GetConfigSection()
  158.         {
  159.             SystemDiagnosticsSection configSection = (SystemDiagnosticsSection)PrivilegedConfigurationManager.GetSection("system.diagnostics");
  160.             return configSection;
  161.         }
  162.        
  163.         static internal bool IsInitializing()
  164.         {
  165.             return initState == InitState.Initializing;
  166.         }
  167.        
  168.         static internal bool IsInitialized()
  169.         {
  170.             return initState == InitState.Initialized;
  171.         }
  172.        
  173.        
  174.         static internal bool CanInitialize()
  175.         {
  176.             return (initState != InitState.Initializing) && !ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress;
  177.         }
  178.        
  179.         static internal void Initialize()
  180.         {
  181.             // Initialize() is also called by other components outside of Trace (such as PerformanceCounter)
  182.             // as a result using one lock for this critical section and another for Trace API critical sections
  183.             // (such as Trace.WriteLine) could potentially lead to deadlock between 2 threads that are
  184.             // executing these critical sections (and consequently obtaining the 2 locks) in the reverse order.
  185.             // Using the same lock for DiagnosticsConfiguration as well as TraceInternal avoids this issue.
  186.             // Sequential locks on TraceInternal.critSec by the same thread is a non issue for this critical section.
  187.             lock (TraceInternal.critSec) {
  188.                
  189.                 // because some of the code used to load config also uses diagnostics
  190.                 // we can't block them while we initialize from config. Therefore we just
  191.                 // return immediately and they just use the default values.
  192.                 if (initState != InitState.NotInitialized || ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress) {
  193.                    
  194.                     return;
  195.                 }
  196.                
  197.                 initState = InitState.Initializing;
  198.                 // used for preventing recursion
  199.                 try {
  200.                     configSection = GetConfigSection();
  201.                 }
  202.                 finally {
  203.                     initState = InitState.Initialized;
  204.                 }
  205.             }
  206.         }
  207.        
  208.         static internal void Refresh()
  209.         {
  210.             ConfigurationManager.RefreshSection("system.diagnostics");
  211.            
  212.             // There might still be some persistant state left behind for
  213.             // ConfigPropertyCollection (for ex, swtichelements), probably for perf.
  214.             // We need to explicitly cleanup any unrecognized attributes that we
  215.             // have added during last deserialization, so that they are re-added
  216.             // during the next Config.GetSection properly and we get a chance to
  217.             // populate the Attributes collection for re-deserialization.
  218.             // Another alternative could be to expose the properties collection
  219.             // directly as Attributes collection (currently we keep a local
  220.             // hashtable which we explicitly need to keep in sycn and hence the
  221.             // cleanup logic below) but the down side of that would be we need to
  222.             // explicitly compute what is recognized Vs unrecognized from that
  223.             // collection when we expose the unrecognized Attributes publically
  224.             SystemDiagnosticsSection configSectionSav = configSection;
  225.             if (configSectionSav != null) {
  226.                
  227.                 if (configSectionSav.Switches != null) {
  228.                     foreach (SwitchElement swelem in configSectionSav.Switches)
  229.                         swelem.ResetProperties();
  230.                 }
  231.                
  232.                 if (configSectionSav.SharedListeners != null) {
  233.                     foreach (ListenerElement lnelem in configSectionSav.SharedListeners)
  234.                         lnelem.ResetProperties();
  235.                 }
  236.                
  237.                 if (configSectionSav.Sources != null) {
  238.                     foreach (SourceElement srelem in configSectionSav.Sources)
  239.                         srelem.ResetProperties();
  240.                 }
  241.             }
  242.            
  243.             configSection = null;
  244.            
  245.             initState = InitState.NotInitialized;
  246.             Initialize();
  247.         }
  248.     }
  249. }

Developer Fusion