Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent LoggingIntegration from registering multiple times #1178

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry.Unity/Integrations/UnityLogHandlerIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/Sentry.Unity.Tests/UnityLogHandlerIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.")));
}
}
}