Skip to content

Commit de68d0f

Browse files
authored
Add DisableSentryNative build property (#4107)
1 parent b0afe1f commit de68d0f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
### Features
66

7+
- Option to disable the SentryNative integration ([#4107](https://github.com/getsentry/sentry-dotnet/pull/4107))
78
- Reintroduced experimental support for Session Replay on Android ([#4097](https://github.com/getsentry/sentry-dotnet/pull/4097))
89

910
### Fixes
11+
1012
- Ensure user exception data is not removed by AspNetCoreExceptionProcessor ([#4016](https://github.com/getsentry/sentry-dotnet/pull/4106))
1113
- Prevent users from disabling AndroidEnableAssemblyCompression which leads to untrappable crash ([#4089](https://github.com/getsentry/sentry-dotnet/pull/4089))
1214
- Fixed MSVCRT build warning on Windows ([#4111](https://github.com/getsentry/sentry-dotnet/pull/4111))

src/Sentry/Platforms/Native/SentryNative.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@ namespace Sentry;
77
internal static class SentryNative
88
{
99
#if NET8_0_OR_GREATER
10-
internal static bool IsAvailable { get; }
10+
// Should be in-sync with Sentry.Native.targets const.
11+
private const string SentryNativeIsEnabledSwitchName = "Sentry.Native.IsEnabled";
12+
13+
private static readonly bool IsAvailableCore;
14+
15+
#if NET9_0_OR_GREATER
16+
// FeatureSwitchDefinition should help with trimming disabled code.
17+
// This way, `SentryNative.IsEnabled` should be treated as a compile-time constant for trimmed apps.
18+
[FeatureSwitchDefinition(SentryNativeIsEnabledSwitchName)]
19+
#endif
20+
private static bool IsEnabled => !AppContext.TryGetSwitch(SentryNativeIsEnabledSwitchName, out var isEnabled) || isEnabled;
21+
22+
internal static bool IsAvailable => IsEnabled && IsAvailableCore;
1123

1224
static SentryNative()
1325
{
14-
IsAvailable = AotHelper.IsTrimmed && !SentryRuntime.Current.IsBrowserWasm();
26+
IsAvailableCore = AotHelper.IsTrimmed && !SentryRuntime.Current.IsBrowserWasm();
1527
}
1628
#else
1729
// This is a compile-time const so that the irrelevant code is removed during compilation.

src/Sentry/Platforms/Native/buildTransitive/Sentry.Native.targets

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
88
Note:Target framework conditions should be kept synchronized with src/Sentry/buildTransitive/Sentry.Native.targets -->
99
<Project>
1010

11+
<ItemGroup>
12+
<!-- When user sets <DisableSentryNative>true</DisableSentryNative> in their project -->
13+
<!-- SentryNative.IsEnabled should result in compile-time constant for trimmed applications -->
14+
<!-- Effectively disabling native library -->
15+
<RuntimeHostConfigurationOption Include="Sentry.Native.IsEnabled"
16+
Condition="'$(DisableSentryNative)' != 'true'"
17+
Value="true"
18+
Trim="true" />
19+
</ItemGroup>
20+
1121
<PropertyGroup>
1222
<!-- net8.0 or greater -->
1323
<FrameworkSupportsNative Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) and ('$(OutputType)' == 'Exe' or '$(OutputType)' == 'WinExe')">true</FrameworkSupportsNative>
24+
<!-- Make it opt-out -->
25+
<FrameworkSupportsNative Condition="'$(DisableSentryNative)' == 'true'">false</FrameworkSupportsNative>
1426
</PropertyGroup>
1527

1628
<ItemGroup Condition="'$(FrameworkSupportsNative)' == 'true' and '$(RuntimeIdentifier)' == 'win-x64'">

0 commit comments

Comments
 (0)