Skip to content

Commit dedcc40

Browse files
Turn Visual and Binding diags on EnableDiagnostics (#29836)
* Turn Visual and Binding diags on EnableDiagnostics more diagnostics to be plugged on this later - fixes #29809 * various fixes * enable diagnostics on tests, others * fix * Apply suggestion from @rmarinho * Apply suggestion from @rmarinho --------- Co-authored-by: Rui Marinho <me@ruimarinho.net>
1 parent 3878008 commit dedcc40

File tree

14 files changed

+162
-49
lines changed

14 files changed

+162
-49
lines changed

docs/design/FeatureSwitches.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The following switches are toggled for applications running on Mono for `TrimMod
1414
| MauiEnableXamlCBindingWithSourceCompilation | Microsoft.Maui.RuntimeFeature.XamlCBindingWithSourceCompilationEnabled | When enabled, MAUI will compile all bindings, including those where the `Source` property is used. |
1515
| MauiHybridWebViewSupported | Microsoft.Maui.RuntimeFeature.IsHybridWebViewSupported | Enables HybridWebView, which makes use of dynamic System.Text.Json serialization features |
1616
| MauiNamescopesSupported | Microsoft.Maui.RuntimeFeature.AreNamescopesSupported | Enable support for Namescopes, FindByName if the application uses it, or to keep supporting runtime and XamlC XAML inflators |
17+
| EnableDiagnostics | Microsoft.Maui.RuntimeFeature.EnableDiagnostics | Enables diagnostic for the running app |
18+
| EnableMauiDiagnostics | Microsoft.Maui.RuntimeFeature.EnableMauiDiagnostics | Enables MAUI specific diagnostics, like VisualDiagnostics and BindingDiagnostics. Defaults to EnableDiagnostics |
1719

1820
## MauiEnableIVisualAssemblyScanning
1921

@@ -84,4 +86,16 @@ With (upcoming) sourcegen XAML inflation, the xaml infrastructure no longer need
8486

8587
Having this off reduce method body size, making them faster to JIT, and release the pressure on the GC as there are way less allocations.
8688

87-
As of NET10.0, the default is `true` so full compatibility is maintained, but might be changed in the future.
89+
As of NET10.0, the default is `true` so full compatibility is maintained, but might be changed in the future.
90+
91+
## EnableDiagnostics
92+
93+
Turn on various diagnostics at runtime and Maui level.
94+
95+
Defaults to `false`
96+
97+
## EnableMauiDiagnostics
98+
99+
Enable VisualDiagnostics and BindingDiagnostics
100+
101+
Defaults to `EnableDiagnostics`

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@
273273
Condition="'$(MauiNamescopesSupported)' != ''"
274274
Value="$(MauiNamescopesSupported)"
275275
Trim="true" />
276+
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.EnableDiagnostics"
277+
Condition="'$(EnableDiagnostics)' != ''"
278+
Value="$(EnableDiagnostics)"
279+
Trim="true" />
276280
</ItemGroup>
277281
</Target>
278282
</Project>

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

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

1616
internal static void SendBindingFailure(BindingBase binding, string errorCode, string message, params object[] messageArgs)
1717
{
18+
if (RuntimeFeature.EnableMauiDiagnostics == false)
19+
{
20+
return;
21+
}
1822
Application.Current?.FindMauiContext()?.CreateLogger<BindingDiagnostics>()?.LogWarning(message, messageArgs);
1923
BindingFailed?.Invoke(null, new BindingBaseErrorEventArgs(VisualDiagnostics.GetSourceInfo(binding), binding, errorCode, message, messageArgs));
2024
}
2125

2226
internal static void SendBindingFailure(BindingBase binding, object source, BindableObject bo, BindableProperty bp, string errorCode, string message, params object[] messageArgs)
2327
{
28+
if (RuntimeFeature.EnableMauiDiagnostics == false)
29+
{
30+
return;
31+
}
2432
Application.Current?.FindMauiContext()?.CreateLogger<BindingDiagnostics>()?.LogWarning(message, messageArgs);
2533
BindingFailed?.Invoke(null, new BindingErrorEventArgs(VisualDiagnostics.GetSourceInfo(binding), binding, source, bo, bp, errorCode, message, messageArgs));
2634
}

src/Controls/tests/Core.UnitTests/BindingUnitTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ namespace Microsoft.Maui.Controls.Core.UnitTests
2020
public class BindingUnitTests
2121
: BindingBaseUnitTests
2222
{
23+
bool enableDiagnosticsInitialState;
24+
public BindingUnitTests()
25+
{
26+
enableDiagnosticsInitialState = RuntimeFeature.EnableDiagnostics;
27+
RuntimeFeature.EnableMauiDiagnostics = true;
28+
}
29+
30+
bool _disposed;
31+
32+
protected virtual void Dispose(bool disposing)
33+
{
34+
if (_disposed)
35+
{
36+
return;
37+
}
38+
39+
if (disposing)
40+
{
41+
RuntimeFeature.EnableMauiDiagnostics = enableDiagnosticsInitialState;
42+
}
43+
44+
_disposed = true;
45+
}
46+
2347
protected override BindingBase CreateBinding(BindingMode mode = BindingMode.Default, string stringFormat = null)
2448
{
2549
return new Binding("Text", mode, stringFormat: stringFormat);

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.EnableMauiDiagnostics = true;
34+
}
35+
36+
[TearDown]
37+
public void TearDown()
38+
{
39+
RuntimeFeature.EnableMauiDiagnostics = 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.EnableMauiDiagnostics = true;
3333
}
3434

3535
[TearDown]
3636
public void TearDown()
3737
{
38-
DebuggerHelper._mockDebuggerIsAttached = debuggerinitialstate;
38+
RuntimeFeature.EnableMauiDiagnostics = 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.EnableMauiDiagnostics = true;
4040
}
4141

4242
[TearDown]
4343
public void TearDown()
4444
{
45-
DebuggerHelper._mockDebuggerIsAttached = _debuggerinitialstate;
45+
RuntimeFeature.EnableMauiDiagnostics = 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.EnableMauiDiagnostics = true;
3535
}
3636

3737
[TearDown]
3838
public void TearDown()
3939
{
40-
DebuggerHelper._mockDebuggerIsAttached = _debuggerinitialstate;
40+
RuntimeFeature.EnableMauiDiagnostics = 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.EnableMauiDiagnostics = true;
36+
}
37+
38+
[TearDown]
39+
public void TearDown()
40+
{
41+
RuntimeFeature.EnableMauiDiagnostics = 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.EnableMauiDiagnostics = true;
25+
}
26+
27+
[TearDown]
28+
public void TearDown()
29+
{
30+
RuntimeFeature.EnableMauiDiagnostics = enableDiagnosticsInitialState;
31+
}
32+
1833
[Test]
1934
public void SourceInfoIsRelative([Values(false)] bool useCompiledXaml)
2035
{

0 commit comments

Comments
 (0)