/// <summary> /// Log locally and conditionally forward to the SpatialOS runtime. /// </summary> /// <param name="type">The type of the log.</param> /// <param name="logEvent">A LogEvent instance.</param> public void HandleLog(LogType type, LogEvent logEvent) { inHandleLog = true; try { if (!string.IsNullOrEmpty(WorkerType)) { logEvent.WithField(LoggingUtils.WorkerType, WorkerType); } if (type == LogType.Exception) { // For exception types, Unity expects an exception object to be passed. // Otherwise, it will not be displayed. var exception = logEvent.Exception ?? new Exception(logEvent.ToString()); Debug.unityLogger.LogException(exception, logEvent.Context); } else { Debug.unityLogger.Log( type, logEvent, logEvent.Context); } var logLevel = LogTypeMapping[type]; if (Worker == null || logLevel < minimumLogLevel) { return; } var message = logEvent.ToString(); if (type == LogType.Exception && logEvent.Exception != null) { // Append the stack trace of the exception to the message. message += $"\n{logEvent.Exception.StackTrace}"; } var entityId = LoggingUtils.ExtractEntityId(logEvent.Data); Worker.SendLogMessage(logLevel, message, LoggingUtils.ExtractLoggerName(logEvent.Data), entityId); } finally { inHandleLog = false; } }
// This method catches exceptions coming from the engine, or third party code. private void LogCallback(string message, string stackTrace, LogType type) { if (inHandleLog) { // HandleLog will do its own message sending. return; } // This is required to avoid duplicate forwarding caused by HandleLog also logging to console if (type == LogType.Exception) { Worker?.SendLogMessage(LogLevel.Error, $"{message}\n{stackTrace}", Worker?.WorkerId, null); } }