diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/AvTrace.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/AvTrace.cs
index 8bb1e7a8b41..2e55406f5e9 100644
--- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/AvTrace.cs
+++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/AvTrace.cs
@@ -51,24 +51,28 @@ public AvTrace( GetTraceSourceDelegate getTraceSourceDelegate, ClearTraceSourceD
PresentationTraceSources.TraceRefresh += new TraceRefreshEventHandler(Refresh);
// Fetch and cache the TraceSource from PresentationTraceSources
- Initialize();
+ Initialize(IsWpfTracingEnabledInRegistry());
}
+ ///
+ /// Performs thread-safe initialization of value.
+ ///
+ static AvTrace() => LoadWpfTracingSettings();
- //
- // Refresh this trace source -- see if it needs to be enabled
- // because registry setting has changed or debugger is attached.
- //
- // To re-read the config file, call System.Diagnostics.Trace.Refresh() .
- //
+ ///
+ /// Refreshes this trace source. Checks again if it has been enabled
+ /// either because registry setting has changed or debugger has been attached.
+ ///
+ /// To re-read the config file, call
+ ///
public void Refresh()
{
- // Cause the registry to be re-read in case it's changed.
- _enabledInRegistry = null;
+ // Re-read current WPF Trace settings from Registry in case it has changed.
+ bool isEnabledInRegistry = LoadWpfTracingSettings();
// Re-initialize everything
- Initialize();
+ Initialize(isEnabledInRegistry);
}
//
@@ -161,12 +165,12 @@ static public void OnRefresh()
//
// Internal initialization
//
- void Initialize( )
+ private void Initialize(bool isEnabledInRegistry)
{
// Decide if we should actually create a TraceSource instance (doing so isn't free,
// so we don't want to do it if we can avoid it).
- if( ShouldCreateTraceSources() )
+ if(ShouldCreateTraceSources(isEnabledInRegistry))
{
// Get TraceSource from the PresentationTraceSources
// (this call will indirectly create the TraceSource if one doesn't already exist)
@@ -175,7 +179,7 @@ void Initialize( )
// We go enabled if tracing is enabled in the registry, if
// PresentationTraceSources.Refresh has been called, or if we're in the debugger
// and the debugger is supposed to enable tracing.
- _isEnabled = IsWpfTracingEnabledInRegistry() || _hasBeenRefreshed || _enabledByDebugger ;
+ _isEnabled = isEnabledInRegistry || _hasBeenRefreshed || _enabledByDebugger ;
}
else
{
@@ -194,52 +198,34 @@ void Initialize( )
// the TraceSource.)
//
- static private bool ShouldCreateTraceSources()
+ private static bool ShouldCreateTraceSources(bool isEnabledInRegistry)
{
- if( IsWpfTracingEnabledInRegistry()
- || IsDebuggerAttached()
- || _hasBeenRefreshed
- )
- {
- return true;
- }
-
- return false;
+ return isEnabledInRegistry || IsDebuggerAttached() || _hasBeenRefreshed;
}
-
-
- ///
- /// Read the registry to see if WPF tracing is allowed
- ///
-
- static internal bool IsWpfTracingEnabledInRegistry()
+ /// Initializes variable from Registry and returns the value.
+ /// A boolean value specifying whether WPF Tracing is enabled.
+ internal static bool LoadWpfTracingSettings()
{
- // First time this is called, initialize from the registry
+ // Initialize from the registry
+ bool enabled = false;
- if( _enabledInRegistry == null )
- {
- bool enabled = false;
+ object keyValue = SecurityHelper.ReadRegistryValue(Registry.CurrentUser,
+ @"Software\Microsoft\Tracing\WPF",
+ "ManagedTracing");
- object keyValue = SecurityHelper.ReadRegistryValue(
- Registry.CurrentUser,
- @"Software\Microsoft\Tracing\WPF",
- "ManagedTracing");
+ if (keyValue is int value && value == 1) //REG_DWORD
+ enabled = true;
- if( keyValue is int && ((int) keyValue) == 1 )
- {
- enabled = true;
- }
+ // Update the static value
+ s_enabledInRegistry = enabled;
- // Update the static. Doing this last protects us from threading problems; worse case, multiple
- // threads will set the same value into it.
- _enabledInRegistry = enabled;
- }
-
- return (bool) _enabledInRegistry;
+ return enabled;
}
-
+ /// Retrieves cached value from to see whether WPF Tracing is enabled.
+ /// A boolean value specifying whether WPF Tracing is enabled.
+ internal static bool IsWpfTracingEnabledInRegistry() => s_enabledInRegistry;
//
// Check for an attached debugger.
@@ -512,8 +498,8 @@ static public Type GetTypeHelper(object value)
// Cache of TraceSource instance; real value resides in PresentationTraceSources.
TraceSource _traceSource;
- // Cache used by IsWpfTracingEnabledInRegistry
- static Nullable _enabledInRegistry = null;
+ // Cache used by IsWpfTracingEnabledInRegistry, written by LoadWpfTracingSettings
+ private static volatile bool s_enabledInRegistry = false;
static char[] FormatChars = new char[]{ '{', '}' };
}