Mando
Mando

Reputation: 11722

Is it possible to add additional information for crashes handled by Xamarin.Insights analytics framework

I have an xamarin.android with xamarin.insights intergrated.

Right now every time I handle error manually (try/catch) I'm adding information about environment (staging/production):

try 
{
    ExceptionThrowingFunction();
} 
catch (Exception exception) 
{ 
    exception.Data["Environment"] = "staging";
    throw;
}

But this information is missing in case if error handled by xamarin.insights itself (in case of crash).

It is possible to add additional exception data in case of crash?

docs reference I used

Upvotes: 0

Views: 261

Answers (1)

Pete
Pete

Reputation: 4746

From reading the docs page reference that you mentioned, I still get the impression that you have to call the .Report method as well as in:-

Insights.Report(exception, new Dictionary <string, string> { 
    {"Some additional info", "foobar"}
}); 

What I believe they are saying in this example:-

try {
    ExceptionThrowingFunction();
} 
catch (Exception exception) { 
    exception.Data["AccountType"] = "standard";
    throw;
}

Is that you have the ability when any Exception is encountered, to package additional information that you can later send to the Insights server, as the Data property of the Exception is just a Key/Value Dictionary.

So if you had an Exception several layers deep, you can choose to re-throw the Exception with additional information contained within it that you will later send to the Insights server.

At a higher level, you can then take the Exception that was thrown deeper down the call-hierarchy and then call the Insights.Report, with:-

Insights.Report( 
    {the rethrown exception in your higher up try..catch block},
    {rethrown exception}.Data 
);

that will then send all the additional Key/Value information previously captured.

From seeing your last part of your question though it looks like you are interested in Insights handling and sending this additional .Data automatically should there be an unhandled exception.

If it is not currently being sent, then perhaps suggest to them that this can be sent also? As it sounds a feasible request for this to automatically be sent as well incase of an unhandled exception.

Update 1:-

Yes - I understand about the unhandled exception scenario now that you are referring to.

I have not dealt with this component directly, so there may be hooks / event handlers or something already defined where you can tap into this, and execute some custom code just prior to this being sent.

If this is not available, then perhaps suggest this to them to include as its a Beta product?

Alternatively, you could still achieve this yourself by capturing the unhandled exceptions just prior to them falling. You'd have to code this however on each platform.

For instance on Windows Phone in the App class there is Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) to which you could then supplement the Exception thrown with this extra .Data.

For Android you could take a look at this post that describes how to catch uncaughtException that will help you in capturing the unhandled exceptions.

Whether just supplementing the Exception in these handlers above is enough all depends on how they've written their hook into this, as to how well it behaves and whether it is executed first, prior to their implementation.

You will have to try and see if it does. If it doesn't behave well, allowing you to supplement extra data prior to the automatic call to Insights, you have another fallback solution, to just do the .Report call manually within these unhandled exception handlers yourself to make this work and supplement the extra .Data to achieve your aim.

Upvotes: 1

Related Questions