【问题标题】:How to export android Unity error and debug logs to file?如何将 android Unity 错误和调试日志导出到文件?
【发布时间】:2017-09-18 06:13:32
【问题描述】:

我正在尝试为三星 Gear VR(使用三星 Galaxy S8)开发一个应用程序,因为它需要我拔出 USB 线才能将手机插入 Gear VR 设备,这不允许我做USB调试。

如何将错误和调试消息导出到我可以阅读的文件中以找出问题所在?

到目前为止,研究表明 android Unity 播放器不会保存到日志文件,而其他平台会这样做,而 adb 是 USB 调试的方式......只有我不能为 Gear 做。

【问题讨论】:

  • 任何努力?执行此操作时卡在某个地方?
  • 一直在 Unity 中查找日志文件管理,到目前为止还没有找到任何进入 Android 的线索。同时仍在研究,但我希望看看到目前为止是否有其他人尝试过这样做。
  • 与此相关的一些细节:尝试通过 URL 从内部存储将视频加载到 Unity VideoPlayer 中,但是虽然我能够将少量调试文本输出到 TextMesh 以确定我的代码是否是工作时,Unity 本身的 VideoPlayer 系统存在一些问题,会在文件准备期间产生错误。
  • 好的。坚持,稍等。我可能会回答。什么统一版本?
  • 最新的。 2017.1.1!

标签: c# android unity3d virtual-reality samsung-galaxy-gear


【解决方案1】:

如何将android Unity错误和调试日志导出到文件?

那还不存在。你必须自己做一个。

1.订阅 Unity 日志事件:

Application.logMessageReceived += LogCallback;

2。将它们存储在一个列表中。当应用程序即将退出时,序列化为 json 并使用File.WriteAllBytes 保存。

保存路径是Application.persistentDataPath。请参阅 this 帖子,该帖子显示了在 Unity 中的任何构建中可以找到路径的位置。

下面是一个示例脚本,它在应用程序即将退出时读取日志并保存它。此外,它还能够通过电子邮件向您发送日志数据。 这需要来自this 帖子的DataSaver 类。 请注意,电子邮件是在下次重新打开应用时发送,而不是在退出时发送。

using System.Collections.Generic;
using UnityEngine;

using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System;
using System.Text;

public class LogSaverAndSender : MonoBehaviour
{
    public bool enableSave = true;
    public bool enableMailing = true;

    public string yourEmail = "fromemail@gmail.com";
    public string yourEmailPassword = "password";
    public string toEmail = "toemail@gmail.com";


    [Serializable]
    public struct Logs
    {
        public string condition;
        public string stackTrace;
        public LogType type;

        public string dateTime;

        public Logs(string condition, string stackTrace, LogType type, string dateTime)
        {
            this.condition = condition;
            this.stackTrace = stackTrace;
            this.type = type;
            this.dateTime = dateTime;
        }
    }

    [Serializable]
    public class LogInfo
    {
        public List<Logs> logInfoList = new List<Logs>();
    }

    LogInfo logs = new LogInfo();

    void OnEnable()
    {
        //Email last saved log
        if (enableMailing)
        {
            mailLog();
        }

        //Subscribe to Log Event
        Application.logMessageReceived += LogCallback;
    }

    //Called when there is an exception
    void LogCallback(string condition, string stackTrace, LogType type)
    {
        //Create new Log
        Logs logInfo = new Logs(condition, stackTrace, type, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));

        //Add it to the List
        logs.logInfoList.Add(logInfo);
    }

    void mailLog()
    {
        //Read old/last saved log
        LogInfo loadedData = DataSaver.loadData<LogInfo>("savelog");
        string date = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");

        //Send only if there is something to actually send
        if (loadedData != null && loadedData.logInfoList != null

            && loadedData.logInfoList.Count > 0)
        {

            Debug.Log("Found log to send!");

            //Convert to json
            string messageToSend = JsonUtility.ToJson(loadedData, true);

            string attachmentPath = Path.Combine(Application.persistentDataPath, "data");
            attachmentPath = Path.Combine(attachmentPath, "savelog.txt");

            //Finally send email
            sendMail(yourEmail, yourEmailPassword, toEmail, "Log: " + date, messageToSend, attachmentPath);

            //Clear old log
            DataSaver.deleteData("savelog");
        }
    }

    void sendMail(string fromEmail, string emaiPassword, string toEmail, string eMailSubject, string eMailBody, string attachmentPath = null)
    {
        try
        {
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(fromEmail);
            mail.To.Add(toEmail);
            mail.Subject = eMailSubject;
            mail.Body = eMailBody;

            if (attachmentPath != null)
            {
                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentPath);
                mail.Attachments.Add(attachment);
            }

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new System.Net.NetworkCredential(fromEmail, emaiPassword) as ICredentialsByHost;
            smtpClient.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback =
                delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return true; };
            smtpClient.Send(mail);
        }
        catch (Exception e) { }
    }

    void OnDisable()
    {
        //Un-Subscribe from Log Event
        Application.logMessageReceived -= LogCallback;
    }

    //Save log  when focous is lost
    void OnApplicationFocus(bool hasFocus)
    {
        if (!hasFocus)
        {
            //Save
            if (enableSave)
                DataSaver.saveData(logs, "savelog");
        }
    }

    //Save log on exit
    void OnApplicationPause(bool pauseStatus)
    {
        if (pauseStatus)
        {
            //Save
            if (enableSave)
                DataSaver.saveData(logs, "savelog");
        }
    }
}

【讨论】:

    【解决方案2】:

    如果你想更好地理解代码,请看我的教程:android files tutorial

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.UI;
    
    
    public class UtilsMath : MonoBehaviour
    {
        private string gpsFilePath;
        private void Start()
        {
          gpsFilePath = Application.persistentDataPath + "/logFilesGPSUnity.txt";
    
          //if you want to empty the file every time the app starts,
          // only delete and create a new one.
    
          // if gps file exists
          if (File.Exists(gpsFilePath))
          {
            //delete file
            try
            {
                File.Delete(gpsFilePath);
                Debug.Log("[Utils Script]: GPS File Log Deleted Successfully!");
            }
            catch (System.Exception e)
            {
                Debug.LogError("[Utils Script]: Cannot delete GPS File Log - Exception: " + e);
            }
          }
      }
    
    
    /// <summary>
    /// <para> Writes the string message into the logFilesGPSUnity.txt in the internal storage\android\data\com.armis.arimarn\files\</para>
    /// You need to write a '\n' in the end or beginning of each message, otherwise, the message will be printed in a row.
    /// </summary>
    /// <param name="message">String to print in the file</param>
      public void WriteToFile(string message)
      {
        try
        {
            //create the stream writer to the specific file
            StreamWriter fileWriter = new StreamWriter(gpsFilePath, true);
    
            //write the string into the file
            fileWriter.Write(message);
    
            // close the Stream Writer
            fileWriter.Close();
        }
        catch (System.Exception e)
        {
            Debug.LogError("[Utils Script]: Cannot write in the GPS File Log - Exception: " + e);
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      相关资源
      最近更新 更多