This topic describes how to use the web tracking feature of Log Service to collect Unity3D logs.

Background information

Unity3D is a cross-platform game engine that is developed by Unity Technologies. The engine allows you to create 3D video games, VR buildings, real-time 3D animation, and other interactive content.

Log Service allows you to use the web tracking feature to collect Unity3D logs. For more information about the web tracking feature, see Use web tracking to collect logs. Unity Debug.Log is used as an example to describe how to collect Unity3D logs.

Procedure

  1. Enable the web tracking feature. For more information, see Use web tracking to collect logs.
  2. Create a Unity3D logging handler.
    In the Unity editor, create a C# file that is named LogOutputHandler.cs, add the following code to the file, and modify the following variables:
    • project: the name of the Log Service project.
    • logstore: the name of the Logstore.
    • serviceAddr: the endpoint of the Log Service 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;
        }
    }

    You can use the preceding script to asynchronously send logs to Log Service. You can also specify other fields in the script to collect the fields.

  3. Generate Unity3D logs.
    Create a file that is named LogglyTest.cs, and add the following code to the file:
    using UnityEngine;
    using System.Collections;
    public class LogglyTest : MonoBehaviour {
        void Start () {
            Debug.Log ("Hello world");
        }
    }
  4. View collected log data.
    After you run the Unity3D application, logs are generated and sent to Log Service. You can view the logs in the Log Service console.