Catch all Exceptions from Android App and Send them to Server or Email

时间:2016-07-11 20:14:53

标签: c# android unity3d

I've got an Unity app in the appstore with a couple hundred downloads now. How can I get all the exceptions that the users triggered? Basically I want to get an email or be able to read the log from somewhere, when a user did something that triggers an exception which I caught.

Sending an email or pushing a log to a server would require extra permissions and I don't know if that's the way to go.

Kind regards

2 个答案:

答案 0 :(得分:2)

Using WWW may require another permission but you need it. You can use SystemInfo to collect relevant system info. Also collect the exception information and store them with WWWForm. Send the WWWForm data with WWW to a php file on your server. You can read the data from php data and save it as a text file. You can also save it to a database then display it in an html page.

The complete example code below will collect crash log and save it as a file.

C#:

 void Start()
 {
     try
     {
         //Do Something Dangerous
     }
     catch (Exception e)
     {
         //send log if Exception is thrown and caught
         sendLog(e);
     }
 }

 public void sendLog(Exception e)
 {
     string errorLogUrl = "http://yourServerUrl.com/gameName/log.php";
     WWWForm errorForm = new WWWForm();

     //Fill in Important Device Info
     errorForm.AddField("Device OS", SystemInfo.operatingSystem);
     errorForm.AddField("Device Model", SystemInfo.deviceModel);
     errorForm.AddField("Graphics Device Name", SystemInfo.graphicsDeviceName);
     errorForm.AddField("Graphics Device Vendor", SystemInfo.graphicsDeviceVendor);
     errorForm.AddField("Graphics Device Version", SystemInfo.graphicsDeviceVersion);
     errorForm.AddField("Graphics Memory Size", SystemInfo.graphicsMemorySize);
     errorForm.AddField("System Memory Size", SystemInfo.systemMemorySize);

     //Fill in current Scene
     errorForm.AddField("Current Scene", UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);

     //Fill in Important Exception Info
     errorForm.AddField("Exc-StackTrace", e.StackTrace);
     errorForm.AddField("Exc-Source", e.Source);
     errorForm.AddField("Exc-Message", e.Message);

     //FileName
     errorForm.AddField("File Name", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss" + UnityEngine.Random.Range(1, 900000)));

     WWW www = new WWW(errorLogUrl, errorForm);
     StartCoroutine(WaitForRequest(www));
 }

 private IEnumerator WaitForRequest(WWW www)
 {
     yield return www;

     //Check if we failed to send
     if (www.error != null)
     {
         //Failed To Send.
     }
 }

Not a php guru. You may have to modify the php code below.

PHP:

<?php

        $DeviceOS = $_POST["Device OS"];
        $DeviceModel = $_POST["Device Model"];
        $GraphicsDeviceName = $_POST["Graphics Device Name"];
        $GraphicsDeviceVendor = $_POST["Graphics Device Vendor"];
        $GraphicsDeviceVersion = $_POST["Graphics Device Version"];
        $GraphicsMemorySize = $_POST["Graphics Memory Size"];
        $SystemMemorySize = $_POST["System Memory Size"];

        $CurrentScene = $_POST["Current Scene"];

        $Exc_StackTrace = $_POST["Exc-StackTrace"];
        $Exc_Source = $_POST["Exc-Source"];     
        $Exc_Message = $_POST["Exc-Message"];       

        $dump_file_name = $_POST["File Name"];

        $finalData = "Device OS: $DeviceOS";
        $finalData = "$finalData\r\nDevice Model: $DeviceModel";
        $finalData = "$finalData\r\nGraphics Device Name: $GraphicsDeviceName";
        $finalData = "$finalData\r\nGraphics Device Vendor: $GraphicsDeviceVendor";
        $finalData = "$finalData\r\nGraphics Device Version: $GraphicsDeviceVersion";
        $finalData = "$finalData\r\nGraphics Memory Size: $GraphicsMemorySize";
        $finalData = "$finalData\r\nSystem Memory Size: $SystemMemorySize";

        $finalData = "$finalData\r\n";

        $finalData = "$finalData\r\nCurrent Scene: $CurrentScene";

        $finalData = "$finalData\r\n";

        $finalData = "$finalData\r\nExc-StackTrace: $Exc_StackTrace";
        $finalData = "$finalData\r\nExc-Source: $Exc_Source";
        $finalData = "$finalData\r\nExc-Message: $Exc_Message";

        $myFile = $dump_file_name . ".txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh,$dump_file_name . "," . $finalData . ",");
        fclose($fh);
?>

答案 1 :(得分:2)

相关问题