Background information

Unity3D is an integrated game development tool  compatible with multiple platforms. Developed by Unity Technologies, this tool allows a player to easily create various interactive contents such as 3D video game, architectural visualization, and real-time 3D animation. Unity3D is a fully integrated and professional game engine.

You can use the Web Tracking function of Log ServiceWeb Tracking  to collect Unity3D logs conveniently.  This document introduces how to use the Web Tracking function to collect Unity logs to Log Service by collecting the Unity Debug.Log .

Procedure

  1. Activate the Web Tracking function
    For more information, see Web Tracking .
  2. Register Unity3D LogHandler
    Create a C# file  LogOutputHandler.cs in the Unity editor. Enter the following codes and modify three member variables in the codes, which are:
    • project, indicating the name of the log project.
    • logstore, indicating the name of the Logstore.
    • serviceAddr, indicating the address of the log project. 

      For more information, see Service endpoint.

    using UnityEngine;
    using System.Collections;
    public class LogOutputHandler : MonoBehaviour
    {
        //Register the HandleLog function on scene start to fire on 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, send logs to Loggly
        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 any User, Game, or Device MetaData that would be useful to finding issues later
            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 preceding codes can asynchronously send logs to Alibaba Cloud Log Service. You can add more fields that you want to collect in the example.

  3. Generate Unity logs
    In the project, create the LogglyTest.cs  file and add the following codes:
    using UnityEngine;
    using System.Collections;
    public class LogglyTest : MonoBehaviour {
        void Start () {
            Debug.Log ("Hello world");
        }
    }
  4. Preview the log in the console.

    After completing the preceding steps, run the Unity program. Then, you can preview your sent logs in the Log Service console.

    The preceding example provides the methods for collecting logs such as Debug.Log, Debug.LogError, and Debug.LogException. The component object model of Unity, its program crash API, and other types of Log APIs can be used to conveniently collect the device information on the client.