0

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

Mando
  • 11,414
  • 17
  • 86
  • 167

1 Answers1

1

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.

Community
  • 1
  • 1
Pete
  • 4,746
  • 2
  • 15
  • 21
  • I want to be subscribed to event when xamarin.insights is going to send information about unhandlede exception to include additional data. Manual .Report() doesn't work for me because I'm not always has control on crashes – Mando Oct 14 '14 at 10:50
  • I wouldn't like to rely on my own implementation of handling unhanlded exceptions, especially with mono and xamarin.android. As long as xamarin.insights already implemented crash handling logic I want to be able to reuse that. Moreover nobody guaranties that my handling logic occurs before xamarin.insights's logic – Mando Oct 14 '14 at 11:14
  • These events are most likely what they are tapping into, in order to provide this functionality. As for 'guaranties that my handling logic occurs before xamarin.insight's logic' - it all depends on how they've written the code, as you can control how things work to a degree. Take a class, as an example, where you create another derived class from and override a method. In this example you then have the control as to whether to execute the base implementation at the start of your override, at the end, etc. Your best bet is to try without making too many assumptions peventing you from trying – Pete Oct 14 '14 at 11:21
  • 1
    As I mentioned though, you should be able to tap into these handlers and supplement the extra **.Data**. You don't necessary have to do the **.Report**. Try it with **only** adding this extra **.Data** and let the function complete as normal. Hopefully the **Insight** handler will then implement the final call? Try and see? – Pete Oct 14 '14 at 11:23
  • I didn't get at what moment should I use .Data property? I don't have access even to exception (and I don't want to subscribe to UnhandledException events). There are many cases which will not be handled by this (like un-awaited or void async calls). I want to make reliable logic as long as it crash analytics I would like to miss something – Mando Oct 14 '14 at 11:26