Search Unity

Custom Metadata for each exception report

Discussion in 'Unity Cloud Diagnostics' started by Djadjouka, May 1, 2019.

  1. Djadjouka

    Djadjouka

    Joined:
    Nov 1, 2016
    Posts:
    6
    Hi,

    Is there a way to set metadata for a crash report just before it's sent? I'm thinking about reporting some data that varies constantly from frame to frame like the framerate, the ping or the time since the start of the game. If there was an event or a callback when a crash report is ready to be sent, that would be perfect. If it doesn't exist, is it something that is on the roadmap?
     
  2. johng_unity

    johng_unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    158
    I think you can achieve this with SetUserMetadata:

    https://docs.unity3d.com/2018.3/Doc...ndler.CrashReportHandler.SetUserMetadata.html

    This post is a little out of date but it describes this feature as well:

    https://blogs.unity3d.com/2018/10/15/whats-new-in-performance-reporting/

    Note this feature of Cloud Diagnostics is only available to Plus/Pro subscribers.
     
  3. Djadjouka

    Djadjouka

    Joined:
    Nov 1, 2016
    Posts:
    6
    Thanks for the quick reply!

    I have tried that this afternoon. My only issue with that method is that I need to keep updating the values each frame instead of being able to set the data when the crash happens to have the most accurate values. For example, if I wanted to send the time since the game has started, I would have to do it like this:

    Code (CSharp):
    1. public class MetadataManager : MonoBehaviour
    2. {
    3.     private static DateTime gameStartTime;
    4.  
    5.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    6.     private void OnGameStarted()
    7.     {
    8.         gameStartTime = DateTime.Now;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         CrashReportHandler.SetUserMetadata("timeSinceGameStart", (DateTime.Now - gameStartTime).TotalSeconds.ToString());
    14.     }
    15. }
    I think that would be easier and more efficient for performance to be able to set the metadata based on a callback or event like this:

    Code (CSharp):
    1. public class MetadataManager : MonoBehaviour
    2. {
    3.     private static DateTime gameStartTime;
    4.  
    5.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    6.     private void OnGameStarted()
    7.     {
    8.         gameStartTime = DateTime.Now;
    9.     }
    10.  
    11.     void Awake()
    12.     {
    13.         CrashReportHandler.UnhandledExceptionEvent += OnUnhandledException;
    14.     }
    15.  
    16.     private void OnUnhandledException()
    17.     {
    18.         CrashReportHandler.SetUserMetadata("timeSinceGameStart", (DateTime.Now - gameStartTime).TotalSeconds.ToString());
    19.     }
    20. }
    The metadata would be processed after that UnhandledExceptionEvent.
     
    emmanuelsolis and Menion-Leah like this.
  4. emmanuelsolis

    emmanuelsolis

    Joined:
    Jan 9, 2019
    Posts:
    1
    I would like add to what @Djadjouka is propossing. Additional to having a callback for when a crash report is genereated, it would also be a great function to be able to force sending a crash report. Some times, part of our code has to be handled in a try ... catch to prevent leaving the execution on an inconsistent state, but it would be very helpfull if we still got the crash report. Currently, only Unhandled exceptions are reported. So, something like this would be great:

    Code (CSharp):
    1.         private void SomeAction()
    2.         {
    3.             try
    4.             {
    5.                 // Work that can fail.
    6.             }
    7.             catch (Exception ex)
    8.             {
    9.                 // Do something to handled the exception
    10.                 CrashReport.Send(ex);
    11.             }
    12.         }
    13.  
    In the example above, after handling the exception, we are still reporting it. That would also be a great place where user metadata can be added to the crash report.
     
    Djadjouka likes this.
  5. ryanc-unity

    ryanc-unity

    Unity Technologies

    Joined:
    Mar 23, 2016
    Posts:
    1,143
    Hi @Djadjouka and @emmanuelsolis, thank you for the awesome suggestions! The callback feature is already in our backlog of features that we'd like to add.

    For the second issue regarding being able to manually send crash reports, you can actually accomplish that by calling Debug.LogException() in your code. These will still send out exception reports while allowing you to properly handle the exception.
     
    Djadjouka likes this.