internal static void Initialize() { // Initialize() is also called by other components outside of Trace (such as PerformanceCounter) // as a result using one lock for this critical section and another for Trace API critical sections // (such as Trace.WriteLine) could potentially lead to deadlock between 2 threads that are // executing these critical sections (and consequently obtaining the 2 locks) in the reverse order. // Using the same lock for DiagnosticsConfiguration as well as TraceInternal avoids this issue. // Sequential locks on TraceInternal.critSec by the same thread is a non issue for this critical section. lock (TraceInternal.critSec) { // because some of the code used to load config also uses diagnostics // we can't block them while we initialize from config. Therefore we just // return immediately and they just use the default values. bool setConfigurationSystemInProgress = (bool)(typeof(ConfigurationManager).GetProperty("SetConfigurationSystemInProgress", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null)); if (s_initState != InitState.NotInitialized || setConfigurationSystemInProgress) { return; } s_initState = InitState.Initializing; // used for preventing recursion try { s_configSection = GetConfigSection(); } finally { s_initState = InitState.Initialized; } } }
internal static void Initialize() { // Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/DiagnosticsConfiguration.cs,188 // This port removed the lock on TraceInternal.critSec since that is now in a separate assembly and TraceInternal // is internal and because GetConfigSection() is not locked elsewhere such as for connection strings. // Because some of the code used to load config also uses diagnostics // we can't block them while we initialize from config. Therefore we just // return immediately and they just use the default values. if (s_initState != InitState.NotInitialized || ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress) { return; } s_initState = InitState.Initializing; // used for preventing recursion try { s_configSection = GetConfigSection(); } finally { s_initState = InitState.Initialized; } }
internal static void Refresh() { ConfigurationManager.RefreshSection("system.diagnostics"); // There might still be some persistant state left behind for // ConfigPropertyCollection (for ex, swtichelements), probably for perf. // We need to explicitly cleanup any unrecognized attributes that we // have added during last deserialization, so that they are re-added // during the next Config.GetSection properly and we get a chance to // populate the Attributes collection for re-deserialization. // Another alternative could be to expose the properties collection // directly as Attributes collection (currently we keep a local // hashtable which we explicitly need to keep in sycn and hence the // cleanup logic below) but the down side of that would be we need to // explicitly compute what is recognized Vs unrecognized from that // collection when we expose the unrecognized Attributes publically SystemDiagnosticsSection configSectionSav = configSection; if (configSectionSav != null) { if (configSectionSav.Switches != null) { foreach (SwitchElement swelem in configSectionSav.Switches) { swelem.ResetProperties(); } } if (configSectionSav.SharedListeners != null) { foreach (ListenerElement lnelem in configSectionSav.SharedListeners) { lnelem.ResetProperties(); } } if (configSectionSav.Sources != null) { foreach (SourceElement srelem in configSectionSav.Sources) { srelem.ResetProperties(); } } } configSection = null; initState = InitState.NotInitialized; Initialize(); }
internal static void Refresh() { ConfigurationManager.RefreshSection("system.diagnostics"); // There might still be some persistant state left behind for // ConfigPropertyCollection (for ex, swtichelements), probably for perf. // We need to explicitly cleanup any unrecognized attributes that we // have added during last deserialization, so that they are re-added // during the next Config.GetSection properly and we get a chance to // populate the Attributes collection for re-deserialization. // Another alternative could be to expose the properties collection // directly as Attributes collection (currently we keep a local // hashtable which we explicitly need to keep in sycn and hence the // cleanup logic below) but the down side of that would be we need to // explicitly compute what is recognized Vs unrecognized from that // collection when we expose the unrecognized Attributes publically SystemDiagnosticsSection configSectionSav = configSection; if (configSectionSav != null) { if (configSectionSav.Switches != null) { foreach (SwitchElement swelem in configSectionSav.Switches) swelem.ResetProperties(); } if (configSectionSav.SharedListeners != null) { foreach (ListenerElement lnelem in configSectionSav.SharedListeners) lnelem.ResetProperties(); } if (configSectionSav.Sources != null) { foreach (SourceElement srelem in configSectionSav.Sources) srelem.ResetProperties(); } } configSection = null; initState = InitState.NotInitialized; Initialize(); }
internal static void Initialize() { // Initialize() is also called by other components outside of Trace (such as PerformanceCounter) // as a result using one lock for this critical section and another for Trace API critical sections // (such as Trace.WriteLine) could potentially lead to deadlock between 2 threads that are // executing these critical sections (and consequently obtaining the 2 locks) in the reverse order. // Using the same lock for DiagnosticsConfiguration as well as TraceInternal avoids this issue. // Sequential locks on TraceInternal.critSec by the same thread is a non issue for this critical section. lock (TraceInternal.critSec) { // because some of the code used to load config also uses diagnostics // we can't block them while we initialize from config. Therefore we just // return immediately and they just use the default values. if ( initState != InitState.NotInitialized || ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress) { return; } initState = InitState.Initializing; // used for preventing recursion try { configSection = GetConfigSection(); } finally { initState = InitState.Initialized; } } }
private static SystemDiagnosticsSection GetConfigSection() { SystemDiagnosticsSection configSection = (SystemDiagnosticsSection)ConfigurationManager.GetSection("system.diagnostics"); return(configSection); }