Collect Unity3D logs

更新时间:
复制 MD 格式

Use the web tracking feature of Simple Log Service to collect Unity3D logs by intercepting Debug.Log output and forwarding it over HTTP.

Overview

Unity3D is a cross-platform game engine developed by Unity Technologies that lets you create interactive content such as 3D video games, VR environments, and real-time 3D animations.

Simple Log Service collects logs from Unity3D games through its web tracking feature. This topic walks through a complete setup using Debug.Log as the log source. For details about web tracking, see Use the web tracking feature to collect logs.

Collect logs from a Unity3D application

Prerequisites

Before you begin, ensure that you have:

  • A Simple Log Service project and Logstore

  • The endpoint for your Simple Log Service project

  • The Unity editor installed

    1. Enable the web tracking feature. For more information, see Use the web tracking feature to collect logs.

    2. Create a Unity3D logging handler.

      In the Unity editor, create a C# file named LogOutputHandler.cs and add the following script. Replace the placeholder variables before running:

      using UnityEngine;
      using System.Collections;
      public class LogOutputHandler : MonoBehaviour
      {
          // Register the HandleLog function on scene start to capture Debug.Log events
          public void OnEnable()
          {
              Application.logMessageReceived += HandleLog;
          }
          // Remove callback when object goes out of scope
          public void OnDisable()
          {
              Application.logMessageReceived -= HandleLog;
          }
          string project = "your project name";
          string logstore = "your logstore name";
          string serviceAddr = "http address of your log service project";
          // Capture Debug.Log output and send logs to Simple Log Service
          public void HandleLog(string logString, string stackTrace, LogType type)
          {
              string parameters = "";
              parameters += "Level=" + WWW.EscapeURL(type.ToString());
              parameters += "&";
              parameters += "Message=" + WWW.EscapeURL(logString);
              parameters += "&";
              parameters += "Stack_Trace=" + WWW.EscapeURL(stackTrace);
              parameters += "&";
              // Add device metadata to help identify issues
              parameters += "Device_Model=" + WWW.EscapeURL(SystemInfo.deviceModel);
              string url = "http://" + project + "." + serviceAddr + "/logstores/" + logstore + "/track?APIVersion=0.6.0&" + parameters;
              StartCoroutine(SendData(url));
          }
          public IEnumerator SendData(string url)
          {
              WWW sendLog = new WWW(url);
              yield return sendLog;
          }
      }

      The script uses a coroutine to send each log entry asynchronously. Each request includes the log level, message text, stack trace, and device model. To collect additional fields, add more parameters to the parameters string following the same pattern.

    3. Generate Unity3D logs.

      Create a file named LogglyTest.cs and add the following script. Attach this component to any GameObject in your scene to trigger a test log on startup:

      using UnityEngine;
      using System.Collections;
      public class LogglyTest : MonoBehaviour {
          void Start () {
              Debug.Log ("Hello world");
          }
      }
    4. View the collected logs.

      Run your Unity3D application. Logs are sent to Simple Log Service as they are generated. To verify successful collection, open the Simple Log Service console and check the Logstore you configured.