(Please make the title of your topic brief but informative, and post error messages under the next heading Details and error message(s))
Description, details and error message(s)
This issue occurs sporadically if the Visual Studio debugger is attached when entering play mode in Unity. The error that is thrown prevents Unity from running until F5 is hit on in Visual Studio, often more than once to progress. The error occurs in the Log method when trying to access StringBuilderCache.Value.
Expected behaviour
This section of code should prevent a blocking error from being thrown.
Screenshots
If applicable, add screenshots to help explain the problem.
How to reproduce
Rate: (Choose one, or describe it yourself) Sometimes
Steps:
Attach the debugger
Enter play mode (happens maybe once or twice per day)
Apologies, but unfortunately not. All the patches on top of 2.1 were only critical fixes, everything else was pushed back to 2.2 for stability reasons.
The good news is, 2.2 is right around the corner as we’re entering the testing phase.
If you need the fix now, you can make the changes directly (requires package embedding if SDK is not coming from the asset store). The file is Coherence.Log/LogTargets/UnityConsoleTarget.cs - here’s the git patch:
diff --git a/Coherence.Log/LogTargets/UnityConsoleTarget.cs b/Coherence.Log/LogTargets/UnityConsoleTarget.cs
index be3a40a82..c0c30ebe1 100644
--- a/Coherence.Log/LogTargets/UnityConsoleTarget.cs
+++ b/Coherence.Log/LogTargets/UnityConsoleTarget.cs
@@ -68,11 +68,12 @@ namespace Coherence.Log.Targets
/// </example>
public bool AddSourceType { get; set; }
- private static readonly ThreadLocal<StringBuilder> StringBuilderCache = new(() => new StringBuilder());
+ [ThreadStatic]
+ private static StringBuilder stringBuilderCache;
public void Log(LogLevel level, string message, (string key, object value)[] args, Logger logger)
{
- var logBuilder = StringBuilderCache.Value;
+ var logBuilder = stringBuilderCache ??= new StringBuilder();
logBuilder.Clear();
if (AddTimestamp)