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.
'프로그래밍 > 유니티' 카테고리의 다른 글
안드로이드 sdk 경로문제 해결방법(Unable to list target platforms.Please make sure the android sdk path is correct) (0) | 2017.03.17 |
---|---|
유니티 비주얼스튜디오로 그래픽 디버그 하는법 (0) | 2017.03.17 |
유니티 비주얼스튜디오 ctrl + ' 단축키 설정법 (0) | 2017.03.14 |
다이나믹 메시 (0) | 2017.01.04 |
이벤트 활용 (0) | 2016.11.11 |