Unity保存日记到 persistentDataPath 目录
发布时间:2024年01月02日
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SaveLog : MonoBehaviour
{
public bool isSavaLog = true;
public bool isSaveError = true;
private void Awake()
{
Application.logMessageReceived += onHandleLog;
string fileDif = Application.persistentDataPath + "/Debug/Error/";
Debug.Log("日记保存路径:" + fileDif);
}
private void onHandleLog(string condition, string stackTrace, LogType type)
{
string time = DateTime.Now.ToString("yyy-MM-dd HH:mm");
LogInfo info = new LogInfo();
info.condition = condition;
info.stackTrace = stackTrace;
info.type = type;
info.tiem = time;
info.deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
info.deviceName = SystemInfo.deviceName;
info.operatingSystem = SystemInfo.operatingSystem;
info.systemMemorySize = SystemInfo.systemMemorySize;
info.deviceModel = SystemInfo.deviceModel;
string jsonStr = JsonUtility.ToJson(info);
if (type == LogType.Error || type == LogType.Exception)
{
string fileDif = Application.persistentDataPath + "/Debug/Error/";
if (isSaveError) writeText(fileDif, jsonStr);
}
else
{
string fileDif = Application.persistentDataPath + "/Debug/Log/";
if (isSavaLog) writeText(fileDif, jsonStr);
}
}
private void writeText(string fileDif, string message)
{
//string fileDif = Application.persistentDataPath + "/Debug/Error/";
if (!Directory.Exists(fileDif))
{
Directory.CreateDirectory(fileDif);
}
string textPath = fileDif + DateTime.Now.ToString("yyy-MM-dd") + ".txt";
if (string.IsNullOrEmpty(textPath) || string.IsNullOrEmpty(message)) return;
StreamWriter sw = null;
if (!File.Exists(textPath)) sw = File.CreateText(textPath);
else sw = File.AppendText(textPath);
sw.WriteLine(message + '\n');
sw.Close();
sw.Dispose();
}
}
[Serializable]
public class LogInfo
{
/// <summary>日志ID</summary>
public string id;
/// <summary>计数</summary>
public int count = 1;
/// <summary>日志条件</summary>
public string condition;
/// <summary>堆栈跟踪信息</summary>
public string stackTrace;
/// <summary>日志类型</summary>
public LogType type;
/// <summary>时间</summary>
public string time;
/// <summary>设备唯一标识符</summary>
public string deviceUniqueIdentifier;
/// <summary>设备名称</summary>
public string deviceName;
/// <summary>操作系统</summary>
public string operatingSystem;
/// <summary>系统内存大小</summary>
public int systemMemorySize;
/// <summary>设备型号</summary>
public string deviceModel;
/// <summary>日记记录时间</summary>
public string tiem;
}
文章来源:https://blog.csdn.net/a451319296/article/details/135344930
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!