From 3252ca857607329dc0510a013cdd3f3128878de4 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 13 Feb 2023 16:46:38 +0100 Subject: [PATCH 1/2] prevent multiple registrations --- .../Integrations/UnityLogHandlerIntegration.cs | 12 ++++++++++++ .../UnityLogHandlerIntegrationTests.cs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs b/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs index c4026e13c..5d499bb8c 100644 --- a/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs +++ b/src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs @@ -28,6 +28,18 @@ public void Register(IHub hub, SentryOptions sentryOptions) { _hub = hub; _sentryOptions = sentryOptions as SentryUnityOptions; + if (_sentryOptions is null) + { + return; + } + + // If called twice (i.e. init with the same options object) the integration will reference itself as the + // original handler loghandler and endlessly forward to itself + if (Debug.unityLogger.logHandler == this) + { + _sentryOptions.DiagnosticLogger?.LogWarning("UnityLogHandlerIntegration has already been registered."); + return; + } _unityLogHandler = Debug.unityLogger.logHandler; Debug.unityLogger.logHandler = this; diff --git a/test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs b/test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs index 0d7b061b9..e0f7c59a8 100644 --- a/test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs +++ b/test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using Sentry.Protocol; using Sentry.Unity.Integrations; +using Sentry.Unity.Tests.SharedClasses; using Sentry.Unity.Tests.Stubs; using UnityEngine; @@ -229,5 +230,21 @@ public void CaptureException_CapturedExceptionAddedAsBreadcrumb() Assert.AreEqual("unity.logger", breadcrumb.Category); Assert.AreEqual(BreadcrumbLevel.Error, breadcrumb.Level); } + + [Test] + public void Register_RegisteredASecondTime_LogsWarningAndReturns() + { + var testLogger = new TestLogger(); + _fixture.SentryOptions.DiagnosticLogger = testLogger; + _fixture.SentryOptions.Debug = true; + var sut = _fixture.GetSut(); + + // Edge-case of initializing the SDK twice with the same options object. + sut.Register(_fixture.Hub, _fixture.SentryOptions); + + Assert.IsTrue(testLogger.Logs.Any(log => + log.logLevel == SentryLevel.Warning && + log.message.Contains("UnityLogHandlerIntegration has already been registered."))); + } } } From e181f1a6ddd4863ae2a24f5a078b0bbd870599ed Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 13 Feb 2023 16:52:28 +0100 Subject: [PATCH 2/2] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c602e1217..9cbf28dd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- Preventing `LoggingIntegration` from registering multiple times ([#1178](https://github.com/getsentry/sentry-unity/pull/1178)) - Fixed the logging integration only capturing tags and missing the message ([#1150](https://github.com/getsentry/sentry-unity/pull/1150)) ### Dependencies