Skip to content

Commit b239375

Browse files
Turn Visual and Binding diags on EnableDiagnostics
more diagnostics to be plugged on this later - fixes #29809
1 parent fe7e229 commit b239375

File tree

12 files changed

+106
-46
lines changed

12 files changed

+106
-46
lines changed

src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@
269269
Condition="'$(MauiHybridWebViewSupported)' != ''"
270270
Value="$(MauiHybridWebViewSupported)"
271271
Trim="true" />
272+
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.EnableDiagnostics"
273+
Condition="'$(EnableDiagnostics)' != ''"
274+
Value="$(EnableDiagnostics)"
275+
Trim="true" />
272276
</ItemGroup>
273277
</Target>
274-
275278
</Project>

src/Controls/src/Core/Xaml/Diagnostics/BindingDiagnostics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public class BindingDiagnostics
1515

1616
internal static void SendBindingFailure(BindingBase binding, string errorCode, string message, params object[] messageArgs)
1717
{
18+
if (RuntimeFeature.EnableDiagnostics == false)
19+
return;
1820
Application.Current?.FindMauiContext()?.CreateLogger<BindingDiagnostics>()?.LogWarning(message, messageArgs);
1921
BindingFailed?.Invoke(null, new BindingBaseErrorEventArgs(VisualDiagnostics.GetSourceInfo(binding), binding, errorCode, message, messageArgs));
2022
}
2123

2224
internal static void SendBindingFailure(BindingBase binding, object source, BindableObject bo, BindableProperty bp, string errorCode, string message, params object[] messageArgs)
2325
{
26+
if (RuntimeFeature.EnableDiagnostics == false)
27+
return;
2428
Application.Current?.FindMauiContext()?.CreateLogger<BindingDiagnostics>()?.LogWarning(message, messageArgs);
2529
BindingFailed?.Invoke(null, new BindingErrorEventArgs(VisualDiagnostics.GetSourceInfo(binding), binding, source, bo, bp, errorCode, message, messageArgs));
2630
}

src/Controls/tests/Xaml.UnitTests/BindingDiagnosticsTests.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ public BindingDiagnosticsTests(bool useCompiledXaml)
2424
#endif
2525
public class Tests
2626
{
27+
bool enableDiagnosticsInitialState;
28+
29+
[SetUp]
30+
public void Setup()
31+
{
32+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
33+
RuntimeFeature.EnableDiagnostics = true;
34+
}
35+
36+
[TearDown]
37+
public void TearDown()
38+
{
39+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
40+
}
41+
2742
[TestCase(false)]
2843
//[TestCase(true)]
2944
public void Test(bool useCompiledXaml)

src/Controls/tests/Xaml.UnitTests/Issues/Gh10803.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ public Gh10803(bool useCompiledXaml)
2020
[TestFixture]
2121
class Tests
2222
{
23-
bool debuggerinitialstate;
23+
bool enableDiagnosticsInitialState;
2424
int failures = 0;
2525

2626
[SetUp]
2727
public void Setup()
2828
{
2929
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
3030
VisualDiagnostics.VisualTreeChanged += VTChanged;
31-
debuggerinitialstate = DebuggerHelper._mockDebuggerIsAttached;
32-
DebuggerHelper._mockDebuggerIsAttached = true;
31+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
32+
RuntimeFeature.EnableDiagnostics = true;
3333
}
3434

3535
[TearDown]
3636
public void TearDown()
3737
{
38-
DebuggerHelper._mockDebuggerIsAttached = debuggerinitialstate;
38+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
3939
DispatcherProvider.SetCurrent(null);
4040
VisualDiagnostics.VisualTreeChanged -= VTChanged;
4141
failures = 0;

src/Controls/tests/Xaml.UnitTests/Issues/Gh11334.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ void Add(object sender, EventArgs e)
3030
[TestFixture]
3131
class Tests
3232
{
33-
bool _debuggerinitialstate;
33+
bool enableDiagnosticsInitialState;
3434

3535
[SetUp]
3636
public void Setup()
3737
{
38-
_debuggerinitialstate = DebuggerHelper._mockDebuggerIsAttached;
39-
DebuggerHelper._mockDebuggerIsAttached = true;
38+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
39+
RuntimeFeature.EnableDiagnostics = true;
4040
}
4141

4242
[TearDown]
4343
public void TearDown()
4444
{
45-
DebuggerHelper._mockDebuggerIsAttached = _debuggerinitialstate;
45+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
4646
VisualDiagnostics.VisualTreeChanged -= OnVTChanged;
4747
}
4848

src/Controls/tests/Xaml.UnitTests/Issues/Gh11335.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ void Add(object sender, EventArgs e)
2525
[TestFixture]
2626
class Tests
2727
{
28-
bool _debuggerinitialstate;
28+
bool enableDiagnosticsInitialState;
2929

3030
[SetUp]
3131
public void Setup()
3232
{
33-
_debuggerinitialstate = DebuggerHelper._mockDebuggerIsAttached;
34-
DebuggerHelper._mockDebuggerIsAttached = true;
33+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
34+
RuntimeFeature.EnableDiagnostics = true;
3535
}
3636

3737
[TearDown]
3838
public void TearDown()
3939
{
40-
DebuggerHelper._mockDebuggerIsAttached = _debuggerinitialstate;
40+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
4141
VisualDiagnostics.VisualTreeChanged -= OnVTChanged;
4242
}
4343

src/Controls/tests/Xaml.UnitTests/Issues/Maui17222.xaml.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ public Maui17222(bool useCompiledXaml)
2525
class Test
2626
{
2727
#if DEBUG
28-
[SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo());
29-
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);
28+
bool enableDiagnosticsInitialState;
29+
30+
[SetUp]
31+
public void Setup()
32+
{
33+
AppInfo.SetCurrent(new MockAppInfo());
34+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
35+
RuntimeFeature.EnableDiagnostics = true;
36+
}
37+
38+
[TearDown]
39+
public void TearDown()
40+
{
41+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
42+
AppInfo.SetCurrent(null);
43+
}
3044

3145
[Test]
3246
public void GetsourceInfo([Values(false)] bool useCompiledXaml)

src/Controls/tests/Xaml.UnitTests/Issues/Maui2418.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ public Maui2418(bool useCompiledXaml)
1515
[TestFixture]
1616
class Tests
1717
{
18+
bool enableDiagnosticsInitialState;
19+
20+
[SetUp]
21+
public void Setup()
22+
{
23+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
24+
RuntimeFeature.EnableDiagnostics = true;
25+
}
26+
27+
[TearDown]
28+
public void TearDown()
29+
{
30+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
31+
}
32+
1833
[Test]
1934
public void SourceInfoIsRelative([Values(false)] bool useCompiledXaml)
2035
{

src/Controls/tests/Xaml.UnitTests/Issues/Maui25608_2.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ public Maui25608_2(bool useCompiledXaml)
2424
class Test
2525
{
2626
EventHandler<BindingBaseErrorEventArgs> _bindingFailureHandler;
27+
bool enableDiagnosticsInitialState;
28+
2729

2830
[SetUp]
2931
public void Setup()
3032
{
3133
Application.SetCurrentApplication(new MockApplication());
3234
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
35+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
36+
RuntimeFeature.EnableDiagnostics = true;
3337
}
3438

3539
[TearDown]
@@ -39,6 +43,7 @@ public void TearDown()
3943
{
4044
BindingDiagnostics.BindingFailed -= _bindingFailureHandler;
4145
}
46+
RuntimeFeature.EnableDiagnostics = enableDiagnosticsInitialState;
4247

4348
AppInfo.SetCurrent(null);
4449
}

src/Core/src/RuntimeFeature.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ namespace Microsoft.Maui
1414
/// </remarks>
1515
internal static class RuntimeFeature
1616
{
17-
private const bool IsIVisualAssemblyScanningEnabledByDefault = false;
18-
private const bool IsShellSearchResultsRendererDisplayMemberNameSupportedByDefault = true;
19-
private const bool IsQueryPropertyAttributeSupportedByDefault = true;
20-
private const bool IsImplicitCastOperatorsUsageViaReflectionSupportedByDefault = true;
21-
private const bool AreBindingInterceptorsSupportedByDefault = true;
22-
private const bool IsXamlCBindingWithSourceCompilationEnabledByDefault = false;
23-
private const bool IsHybridWebViewSupportedByDefault = true;
17+
const bool IsIVisualAssemblyScanningEnabledByDefault = false;
18+
const bool IsShellSearchResultsRendererDisplayMemberNameSupportedByDefault = true;
19+
const bool IsQueryPropertyAttributeSupportedByDefault = true;
20+
const bool IsImplicitCastOperatorsUsageViaReflectionSupportedByDefault = true;
21+
const bool AreBindingInterceptorsSupportedByDefault = true;
22+
const bool IsXamlCBindingWithSourceCompilationEnabledByDefault = false;
23+
const bool IsHybridWebViewSupportedByDefault = true;
24+
const bool EnableDiagnosticsByDefault = false;
2425

2526
#pragma warning disable IL4000 // Return value does not match FeatureGuardAttribute 'System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute'.
2627
#if NET9_0_OR_GREATER
2728
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsIVisualAssemblyScanningEnabled")]
2829
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
2930
#endif
30-
internal static bool IsIVisualAssemblyScanningEnabled =>
31+
public static bool IsIVisualAssemblyScanningEnabled =>
3132
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsIVisualAssemblyScanningEnabled", out bool isEnabled)
3233
? isEnabled
3334
: IsIVisualAssemblyScanningEnabledByDefault;
@@ -36,7 +37,7 @@ internal static class RuntimeFeature
3637
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsShellSearchResultsRendererDisplayMemberNameSupported")]
3738
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
3839
#endif
39-
internal static bool IsShellSearchResultsRendererDisplayMemberNameSupported
40+
public static bool IsShellSearchResultsRendererDisplayMemberNameSupported
4041
=> AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsShellSearchResultsRendererDisplayMemberNameSupported", out bool isSupported)
4142
? isSupported
4243
: IsShellSearchResultsRendererDisplayMemberNameSupportedByDefault;
@@ -45,7 +46,7 @@ internal static bool IsShellSearchResultsRendererDisplayMemberNameSupported
4546
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsQueryPropertyAttributeSupported")]
4647
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
4748
#endif
48-
internal static bool IsQueryPropertyAttributeSupported =>
49+
public static bool IsQueryPropertyAttributeSupported =>
4950
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsQueryPropertyAttributeSupported", out bool isSupported)
5051
? isSupported
5152
: IsQueryPropertyAttributeSupportedByDefault;
@@ -54,23 +55,23 @@ internal static bool IsShellSearchResultsRendererDisplayMemberNameSupported
5455
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsImplicitCastOperatorsUsageViaReflectionSupported")]
5556
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
5657
#endif
57-
internal static bool IsImplicitCastOperatorsUsageViaReflectionSupported =>
58+
public static bool IsImplicitCastOperatorsUsageViaReflectionSupported =>
5859
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsImplicitCastOperatorsUsageViaReflectionSupported", out bool isSupported)
5960
? isSupported
6061
: IsImplicitCastOperatorsUsageViaReflectionSupportedByDefault;
6162

6263
#if NET9_0_OR_GREATER
6364
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.Microsoft.Maui.RuntimeFeature.AreBindingInterceptorsSupported")]
6465
#endif
65-
internal static bool AreBindingInterceptorsSupported =>
66+
public static bool AreBindingInterceptorsSupported =>
6667
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.AreBindingInterceptorsSupported", out bool areSupported)
6768
? areSupported
6869
: AreBindingInterceptorsSupportedByDefault;
6970

7071
#if NET9_0_OR_GREATER
7172
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsXamlCBindingWithSourceCompilationEnabled")]
7273
#endif
73-
internal static bool IsXamlCBindingWithSourceCompilationEnabled =>
74+
public static bool IsXamlCBindingWithSourceCompilationEnabled =>
7475
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsXamlCBindingWithSourceCompilationEnabled", out bool areSupported)
7576
? areSupported
7677
: IsXamlCBindingWithSourceCompilationEnabledByDefault;
@@ -80,10 +81,29 @@ internal static bool IsShellSearchResultsRendererDisplayMemberNameSupported
8081
[FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
8182
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
8283
#endif
83-
internal static bool IsHybridWebViewSupported =>
84+
public static bool IsHybridWebViewSupported =>
8485
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsHybridWebViewSupported", out bool isSupported)
8586
? isSupported
8687
: IsHybridWebViewSupportedByDefault;
88+
89+
#if NET9_0_OR_GREATER
90+
[FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.EnableDiagnostics")]
91+
#endif
92+
public static bool EnableDiagnostics
93+
{
94+
get
95+
{
96+
return AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.EnableDiagnostics", out bool isEnabled)
97+
? isEnabled
98+
: EnableDiagnosticsByDefault;
99+
}
100+
internal set
101+
{
102+
// This property is internal settable to allow tests to enable diagnostics.
103+
// It should not be set in production code.
104+
AppContext.SetSwitch("Microsoft.Maui.RuntimeFeature.EnableDiagnostics", value);
105+
}
106+
}
87107
#pragma warning restore IL4000
88108
}
89109
}

0 commit comments

Comments
 (0)