Skip to content

fix: harden the UnityLogger from throwing while logging - #2832

Merged
bitsandfoxes merged 2 commits into
mainfrom
fix/unity-logger-throws-while-logging
Sep 7, 2026
Merged

fix: harden the UnityLogger from throwing while logging#2832
bitsandfoxes merged 2 commits into
mainfrom
fix/unity-logger-throws-while-logging

Conversation

@bitsandfoxes

@bitsandfoxes bitsandfoxes commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

The bug

A diagnostic logger must never take its caller down with it. I.e. when logging from inside a catch that has already handled the failure. Throwing inside the catch because logging? You're in for a bad time.

UnityLogger.Log interpolated the exception directly:

_logger.Log(GetUnityLogType(logLevel), LogTag, $"({logLevel}) {Format(message, args)} {exception}");

So anything that threw while being stringified escaped from within the handler.

The fix

Format the message and render the exception defensively, and never let the write to Unity's logger propagate. The exception falls back to type and message when the stack trace cannot be read, so the error stays legible instead of disappearing.

Example:

Sentry: (Error) Failed to resolve hardware installation ID.
  System.TypeInitializationException: The type initializer for
  'System.Net.NetworkInformation.SystemNetworkInterface' threw an exception.
  (stack trace unavailable: TypeInitializationException)
Sentry: (Debug) Registering integration: 'UnityLogHandlerIntegration'.
...

Callers log from inside catch blocks that have already handled the failure.
UnityLogger interpolated the exception directly, so anything that threw while
being stringified escaped from within the handler and turned a handled error
into a fatal one.

On Nintendo Switch that is what happened: with DisableFileWrite set, resolving
the installation ID falls through to NetworkInterface.GetAllNetworkInterfaces(),
whose static initializer throws. Hub.ConfigureScope caught it and logged it, but
stringifying that exception threw again, escaping the handler and aborting SDK
initialization partway through registering integrations. Every Unity integration
after GlobalRootScopeIntegration was left unregistered, so nothing was captured
and no envelope was ever produced.

Format the message and render the exception defensively, falling back to the
type and message when the stack trace cannot be read, and never let the write
to Unity's logger propagate.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ABHTqM37qGAc9iQZcdbdY8
@bitsandfoxes bitsandfoxes changed the title fix: stop the diagnostic logger from throwing while logging fix: harden the diagnostic logger from throwing while logging Sep 7, 2026
@bitsandfoxes bitsandfoxes changed the title fix: harden the diagnostic logger from throwing while logging fix: harden the UnityLogger from throwing while logging Sep 7, 2026
@bitsandfoxes
bitsandfoxes merged commit 00a06a7 into main Sep 7, 2026
114 of 117 checks passed
@bitsandfoxes
bitsandfoxes deleted the fix/unity-logger-throws-while-logging branch September 7, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants