diff --git a/CHANGELOG.md b/CHANGELOG.md index 125faac97..8294facd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Hub.IsEnabled check in logging integration ([#210](https://github.com/getsentry/sentry-unity/pull/210)) + ### Features - Offline caching ([#208](https://github.com/getsentry/sentry-unity/pull/208)) diff --git a/src/Sentry.Unity/Integrations/UnityApplicationLoggingIntegration.cs b/src/Sentry.Unity/Integrations/UnityApplicationLoggingIntegration.cs index 13d61cb2d..e683980f0 100644 --- a/src/Sentry.Unity/Integrations/UnityApplicationLoggingIntegration.cs +++ b/src/Sentry.Unity/Integrations/UnityApplicationLoggingIntegration.cs @@ -35,6 +35,11 @@ public void Register(IHub hub, SentryOptions sentryOptions) // Internal for testability internal void OnLogMessageReceived(string condition, string stackTrace, LogType type) { + if (_hub is null || !_hub.IsEnabled) + { + return; + } + var debounced = type switch { LogType.Error or LogType.Exception or LogType.Assert => ErrorTimeDebounce.Debounced(), @@ -42,7 +47,7 @@ internal void OnLogMessageReceived(string condition, string stackTrace, LogType LogType.Warning => WarningTimeDebounce.Debounced(), _ => true }; - if (!debounced || _hub is null) + if (!debounced) { return; } diff --git a/test/Sentry.Unity.Tests/IntegrationTests.cs b/test/Sentry.Unity.Tests/IntegrationTests.cs index 6207049aa..06eac860b 100644 --- a/test/Sentry.Unity.Tests/IntegrationTests.cs +++ b/test/Sentry.Unity.Tests/IntegrationTests.cs @@ -142,6 +142,34 @@ public IEnumerator BugFarmScene_EventCaptured_IncludesApplicationInEditorOrProdu actual.Environment); } + [UnityTest] + public IEnumerator BugFarmScene_MultipleSentryInit_SendEventForTheLatest() + { + yield return SetupSceneCoroutine("1_BugFarmScene"); + + var sourceEventCapture = new TestEventCapture(); + var sourceDsn = "https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417"; + SentryUnity.Init(options => + { + options.Dsn = sourceDsn; + options.AddIntegration(new UnityApplicationLoggingIntegration(eventCapture: sourceEventCapture)); + }); + + var nextEventCapture = new TestEventCapture(); + var nextDsn = "https://a520c186ed684a8aa7d5d334bd7dab52@o447951.ingest.sentry.io/5801250"; + SentryUnity.Init(options => + { + options.Dsn = nextDsn; + options.AddIntegration(new UnityApplicationLoggingIntegration(eventCapture: nextEventCapture)); + }); + + var testBehaviour = new GameObject("TestHolder").AddComponent(); + testBehaviour.gameObject.SendMessage(nameof(testBehaviour.TestException)); + + Assert.AreEqual(0, sourceEventCapture.Events.Count, sourceDsn); + Assert.AreEqual(1, nextEventCapture.Events.Count, nextDsn); + } + private static IEnumerator SetupSceneCoroutine(string sceneName) { // load scene with initialized Sentry, SceneManager.LoadSceneAsync(sceneName);