Unity 打包后的日志文件

using System;
using System.Text;
using UnityEngine;
using System.IO;
using System.Diagnostics;
public sealed class DebugMonitor : MonoBehaviour
{
	//
	private StringBuilder _builder = new StringBuilder();
	
	//文件保存路径
	private string _logPath = "";
	 
	 //是否自动清理
	 public bool isAutoClear = true;
	
	 //多久后清理
	 public int maxDay = 2;
	
	 private void Start()
	 {
#if UNITY_EDITOR
        return;
#endif
		Init();
	 }	
	 private void OnDestroy()
	 {
		 Application.logMessageReceived -= LogMessage;
         Application.quitting -= ApplicationQuit;
	 }
	
	 //初始化
	 private void Init()
	 {
	 	 //不同平台下,设置不同路径
	 	 _logPath = Application.streamingAssetsPath + "/DebugMessage.txt";
		
		 // debug 日志 回调
         Application.logMessageReceived += LogMessage;
  
  		 //程序退出中 回调
         Application.quitting += ApplicationQuit;
         
		 //
		 if(isAutoClear && File.Exists(_logPath))
		 {
		 	try
            {
                DateTime _lastTime = File.GetLastWriteTime(_logPath);
                if ((DateTime.Now - _lastTime).TotalDays > maxDay)
                {
                    DeleteFile(_logPath);
                }
            }
            catch { }
		 }
	 }
     
    /// <summary>
    /// 程序退出
    /// </summary>
    private void ApplicationQuit()
    {
        //
        _builder.Append(DateTime.Now + "-----------------------退出程序---------------------------" + "\n");

        //
        SaveToTxt(_logPath, _builder.ToString());
    }
    
    /// <summary>
    /// 实时监听 打印日志
    /// </summary>
    /// <param name="condition"></param>
    /// <param name="stackTrace"></param>
    /// <param name="type"></param>
    private void LogMessage(string condition, string stackTrace, LogType type)
    {
        //string all = string.Format("第一个参数 : {0} 。 第二个参数 : {1} 。 第三个参数 : {2}", condition, stackTrace, type.ToString());

        _builder.Append(type);
        _builder.Append("->");
        _builder.Append("Info->");
        _builder.Append(condition);
        _builder.Append("\n");

        //获取堆栈调用信息
        StackTrace st = new StackTrace(true);
        var frames = st.GetFrames();

        for (int i = 0; i < frames.Length; i++)
        {
            _builder.Append(i);
            _builder.Append("\n");

            //方法
            var method = frames[i].GetMethod();
            _builder.Append("Method->");
            _builder.Append(method);
            _builder.Append("\n");

            //文件类
            var fileName = frames[i].GetFileName();
            _builder.Append("File->");
            _builder.Append(fileName);
            _builder.Append("\n");
        }

        _builder.Append("\n");
    }
    
    private void DeleteFile(string filePath)
    {
 		if (File.Exists(filePath))
 		{
 			try
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
                File.Delete(filePath);
            }
            catch (IOException e)
            {
                Debug.LogError(e.Message + "\n" + filePath);
            }
		}
 	}
	
	private bool SaveToTxt(string filePath,string source)
	{
	    if (string.IsNullOrWhiteSpace(filePath))
        {
             Debug.Log("filePath is null");
             return false;
        }
        
        if (source == null)
        {
             Debug.Log("source is not allowed to be null");
             return false;
        }
		
		try
        {
             using(FileStream stream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
             {
                  byte[] arr = Encoding.UTF8.GetBytes(source);
                  stream.Write(arr,0,arr.Length);
             }
        }
        catch (Exception e)
        {
              Debug.LogError(e.Message);
              return false;
         }

         Debug.Log(source.GetType() + " Txt Save Success~");
         return true;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值