Serilog.Sinks.Unity3D
Serilog.Sinks.Unity3D copied to clipboard
.ForContext(this) causes issues with sinks that use different threads, such as SEQ
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.