acastrodev
2/19/2015 - 1:19 PM

Exception Logger

Exception Log

using UnityEngine;
using System.Collections;
using System.IO;

public class Logger : MonoBehaviour
{
    public string saveFile = Path.Combine(Application.persistentDataPath, string.Format("Log{0}.txt", DateTime.Now.ToString("yyyyMMddHHmmssffff")));
    private StringWriter logWriter;

    void OnEnable()
    {
        Application.RegisterLogCallback(ExceptionWriter);
    }

    void OnDisable()
    {
        Application.RegisterLogCallback(null);
    }

    void ExceptionWriter(string message, string stackTrace, LogType type)
    {
        switch (type)
        {
            case LogType.Exception:
            case LogType.Error:
                using (StreamWriter writer = new StreamWriter(new FileStream(saveFile, FileMode.Append)))
                {
                    writer.WriteLine(type);
                    writer.WriteLine(message);
                    writer.WriteLine(stackTrace);
                }
                break;
            default:
                break;
        }
    }
}