DiagnosticsConfiguration
using System.Configuration;
namespace System.Diagnostics
{
internal static class DiagnosticsConfiguration
{
private enum InitState
{
NotInitialized,
Initializing,
Initialized
}
private static volatile SystemDiagnosticsSection s_configSection;
private static volatile InitState s_initState;
internal static SwitchElementsCollection SwitchSettings {
get {
Initialize();
return s_configSection?.Switches;
}
}
internal static string ConfigFilePath {
get {
Initialize();
SystemDiagnosticsSection systemDiagnosticsSection = s_configSection;
if (systemDiagnosticsSection != null)
return systemDiagnosticsSection.ElementInformation.Source;
return string.Empty;
}
}
internal static bool AutoFlush {
get {
Initialize();
SystemDiagnosticsSection systemDiagnosticsSection = s_configSection;
if (systemDiagnosticsSection != null && systemDiagnosticsSection.Trace != null)
return systemDiagnosticsSection.Trace.AutoFlush;
return false;
}
}
internal static bool UseGlobalLock {
get {
Initialize();
SystemDiagnosticsSection systemDiagnosticsSection = s_configSection;
if (systemDiagnosticsSection != null && systemDiagnosticsSection.Trace != null)
return systemDiagnosticsSection.Trace.UseGlobalLock;
return true;
}
}
internal static int IndentSize {
get {
Initialize();
SystemDiagnosticsSection systemDiagnosticsSection = s_configSection;
if (systemDiagnosticsSection != null && systemDiagnosticsSection.Trace != null)
return systemDiagnosticsSection.Trace.IndentSize;
return 4;
}
}
internal static ListenerElementsCollection {
get {
Initialize();
return s_configSection?.SharedListeners;
}
}
internal static SourceElementsCollection Sources {
get {
Initialize();
return s_configSection?.Sources;
}
}
internal static SystemDiagnosticsSection SystemDiagnosticsSection {
get {
Initialize();
return s_configSection;
}
}
private static SystemDiagnosticsSection GetConfigSection()
{
return s_configSection ?? (s_configSection = (SystemDiagnosticsSection)PrivilegedConfigurationManager.GetSection("system.diagnostics"));
}
internal static bool IsInitializing()
{
return s_initState == InitState.Initializing;
}
internal static bool IsInitialized()
{
return s_initState == InitState.Initialized;
}
internal static bool CanInitialize()
{
if (s_initState != InitState.Initializing)
return !ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress;
return false;
}
internal static void Initialize()
{
if (s_initState == InitState.NotInitialized && !ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress) {
s_initState = InitState.Initializing;
try {
s_configSection = GetConfigSection();
} finally {
s_initState = InitState.Initialized;
}
}
}
internal static void Refresh()
{
ConfigurationManager.RefreshSection("system.diagnostics");
SystemDiagnosticsSection systemDiagnosticsSection = s_configSection;
if (systemDiagnosticsSection != null) {
if (systemDiagnosticsSection.Switches != null) {
foreach (SwitchElement switch in systemDiagnosticsSection.Switches) {
switch.ResetProperties();
}
}
if (systemDiagnosticsSection.SharedListeners != null) {
foreach (ListenerElement sharedListener in systemDiagnosticsSection.SharedListeners) {
sharedListener.ResetProperties();
}
}
if (systemDiagnosticsSection.Sources != null) {
foreach (SourceElement source in systemDiagnosticsSection.Sources) {
source.ResetProperties();
}
}
}
s_configSection = null;
s_initState = InitState.NotInitialized;
Initialize();
}
}
}