KuraiAndras / Serilog.Sinks.Unity3D

Serilog Sink for Unity 3D Debug log
MIT License
42 stars 9 forks source link

.ForContext(this) causes issues with sinks that use different threads, such as SEQ #13

Open HaydenReeve opened 1 month ago

HaydenReeve commented 1 month ago

When writing to other sinks, the following error occurs when attempting to use a .ForContext(this) call on a Monobehaviour:

Event with message template Second could not be formatted as JSON
UnityEngine.UnityException: ToString can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
  at (wrapper managed-to-native) UnityEngine.Object.ToString_Injected(intptr,UnityEngine.Bindings.ManagedSpanWrapper&)
  at UnityEngine.Object.ToString (UnityEngine.Object obj) [0x00006] in <6b66e7caaeb045048a0fbc11f111e6fa>:0 
  at UnityEngine.Object.ToString () [0x00001] in <6b66e7caaeb045048a0fbc11f111e6fa>:0 
  at Serilog.Formatting.Json.JsonValueFormatter.FormatLiteralObjectValue (System.Object value, System.IO.TextWriter output) [0x0000c] in <a9e0c87400384258a29ca101b648c38f>:0 
  at Serilog.Formatting.Json.JsonValueFormatter.FormatLiteralValue (System.Object value, System.IO.TextWriter output) [0x001ad] in <a9e0c87400384258a29ca101b648c38f>:0 
  at Serilog.Formatting.Json.JsonValueFormatter.VisitScalarValue (System.IO.TextWriter state, Serilog.Events.ScalarValue scalar) [0x00013] in <a9e0c87400384258a29ca101b648c38f>:0 
  at Serilog.Data.LogEventPropertyValueVisitor`2[TState,TResult].Visit (TState state, Serilog.Events.LogEventPropertyValue value) [0x00016] in <a9e0c87400384258a29ca101b648c38f>:0 
  at Serilog.Formatting.Json.JsonValueFormatter.Format (Serilog.Events.LogEventPropertyValue value, System.IO.TextWriter output) [0x00000] in <a9e0c87400384258a29ca101b648c38f>:0 
  at Serilog.Sinks.Seq.SeqCompactJsonFormatter.FormatEvent (Serilog.Events.LogEvent logEvent, System.IO.TextWriter output, Serilog.Formatting.Json.JsonValueFormatter valueFormatter) [0x00406] in <4b7b01c25fc74f9c8003704b981f39d4>:0 
  at Serilog.Sinks.Seq.SeqCompactJsonFormatter.Format (Serilog.Events.LogEvent logEvent, System.IO.TextWriter output) [0x00000] in <4b7b01c25fc74f9c8003704b981f39d4>:0 
  at Serilog.Sinks.Seq.ConstrainedBufferedFormatter.Format (Serilog.Events.LogEvent logEvent, System.IO.TextWriter output, System.Boolean writePlaceholders) [0x00006] in <4b7b01c25fc74f9c8003704b981f39d4>:0 

I didn't find any documentation about this limitation, so it may be worth putting some XML documentation within the context call.

The resolution is to log to the sub logger with this specific context, but it does get a bit arbitrary when you dig into it. One of the main advantages to using Serilog over Unity.Test structured logging is the more general sink support.

KuraiAndras commented 4 weeks ago

Well, this will happen with any sink which uses a background thread to run. If this happens with this library I don't think I can do anythig. My advice would be to not use the ForContext method with loggers that can run on the background thread

HaydenReeve commented 4 weeks ago

Yeah, I wasn't suggesting it needs to be handled, simply that it might be worth a warning on the call. Something like a /// <remarks> letting you know that it's only safe for main thread sinks. Caught me off guard the first time it happened, but once I realised what was going on it made sense.

Otherwise, it works with no issues.