프로그래밍/유니티2017. 3. 17. 01:38

public class ExceptionLog : MonoBehaviour

{

    private System.IO.StreamWriter SW;

    private string LogFileNmae = "PYJ.txt";

    

    // Use this for initialization

    void Start ()

    {

        SW = new System.IO.StreamWriter(Application.persistentDataPath + "/" + LogFileNmae);

    }

    private void OnEnable()

    {

        Application.logMessageReceived+=HandleLog;

    }

    // Update is called once per frame

    void Update()

    {

//예외 의도적 발생 (0으로 나눔)

        if (Input.GetKeyDown(KeyCode.Return))

        {

            int x = 10;

            int z = 0;

            int k = x / z;

            Destroy(this.gameObject, 1.0f);

        }

}

    private void OnDisable()

    {       

        Application.logMessageReceived += null;

    }

    void HandleLog(string logString,string stackTrace,LogType type)

    {

        if (type == LogType.Exception || type == LogType.Error)

        {          

            SW.WriteLine("Logged at : " + System.DateTime.Now.ToString()

                + " -  Log Desc: " + logString

                + " - Trace: " + stackTrace

                + " - Type: " + type.ToString()

                );

        }

    }


    private void OnDestroy()

    {

        Debug.Log("기록완료");

        SW.Close();

    }

}


핵심 : HandleLog,딜리게이트를 이용해 예외가 발생했을 경우  Application.persistentDataPath 경로에 오류 내용을 기록.

빌드후 버그를 확인할때 사용하면 좋음


참고 


Application.logMessageReceived

Description

Event that is fired if a log message is received.

This event only ever triggers on the main thread. Use it if your handler requires accessing parts of the Unity API that restricted to the main thread or if for other reasons your handler is not thread-safe.


Posted by JinFluenza