美文网首页unity3D技术分享
unity保存log堆栈到本地

unity保存log堆栈到本地

作者: 好怕怕 | 来源:发表于2019-02-28 13:34 被阅读15次
    
    public class Test : MonoBehaviour
    {
    
        //文件的路径
        public string path;
        StreamWriter writer;
        StreamReader reader;
    
        void Start()
        {
            SetPath();
    
            // 方法一
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                // file.Delete();
                //  file.Refresh();
            }
    
            // 方法二
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
    
    
        void Update()
        {
            if (Input.GetMouseButtonUp(0))
            {
                Debug.Log("Log");
                Debug.LogError("LogError");
                Debug.LogWarning("LogError");
            }
        }
    
    
        void OnEnable()
        {
            Application.logMessageReceivedThreaded += OnLogMessageReceivedThreaded;
            System.AppDomain.CurrentDomain.UnhandledException += _OnUnresolvedExceptionHandler;
        }
    
        void OnDisable()
        {
            Application.logMessageReceivedThreaded -= OnLogMessageReceivedThreaded;
            System.AppDomain.CurrentDomain.UnhandledException -= _OnUnresolvedExceptionHandler;
    
        }
    
        private void OnLogMessageReceivedThreaded(string condition, string stackTrace, LogType type)
        {
            StringBuilder str = new StringBuilder();
            str.Append(type.ToString() + ":" + condition + "    堆栈信息:" + stackTrace);
            //   WriteIntoTxt(str.ToString());
            WriteIntoTxtTown(str.ToString());
        }
    
        private void _OnUnresolvedExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            Debug.LogError(sender);
        }
    
        // 方法一
        public void WriteIntoTxt(string message)
        {
            FileInfo file = new FileInfo(path);
            if (!file.Exists)
            {
                writer = file.CreateText();
            }
            else
            {
                writer = file.AppendText();
            }
            writer.WriteLine(message);
            writer.Flush();
            writer.Dispose();
            writer.Close();
        }
    
        // 方法二
        public void WriteIntoTxtTown(string message)
        {
            File.AppendAllText(path, message);
        }
    
    
    
        void SetPath()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                path = Application.persistentDataPath + "/logInfo.txt";
            }
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                path = Application.streamingAssetsPath + "/logInfo.txt";
            }
        }
    }
    
    
    

    相关文章

      网友评论

        本文标题:unity保存log堆栈到本地

        本文链接:https://www.haomeiwen.com/subject/svunxqtx.html