Adding to default uncaught exception error tags

Sam

Posted on
Jul 30 2014

Hi there,

I was wondering what the best way (if any) to change the default tags that are sent with any uncaught exception.

I know these are set during initialisation with the method: RaygunClient.AttachExceptionHandler(tags);

However, we want to be able to add to these default tags through-out the app so we can gain more information about the current state of the app when uncaught exceptions occur.

Would it be best to override the RaygunClient.AttachExceptionHandler(tags); method and make it always create a new RaygunUncaughtExceptionHandler (remove the ! ). That way when we want to update the tags, we call that method and a new Handler will be set with the updated tags.

Looking forward to your response. Thanks!


Jason Fauchelle

Raygun

Posted on
Aug 12 2014

Hello Sam

Overriding the AttachExceptionHandler method is perfectly fine and would be the easiest solution. We may even remove that condition in a future version to make this easier. Note however that the RaygunUncaughtException handler is a private class, so you'd either need to copy our code into your own class, OR set the default exception handler to be a dummy handler which will cause the condition in the AttachExceptionHandler method to pass.

Just to let you know: Another option for sending tags is to send the exceptions manually rather than causing Raygun to set the exception handler. To do this, attach your own exception handler, and create an instance of the RaygunClient. Then you can call the Send method overload that takes a list of tags. This may be useful if your tags change quite often, such as if they are mostly related to the exception being sent. If your tags are mostly the same for all exceptions being sent, then using AttachExceptionHandler as described above would be best.

Please let me know if you have further questions about this.

-Jason Fauchelle


gauche

Posted on
Apr 19 2017

Sorry to necro an old thread, but is this advice still relevant? I'm trying to do exactly the same thing so that crash reports include a tag telling me what environment the app is running in (production versus integration). But I can't for the life of me see a way to easily set some default tags. Is there any documentation on this?

UPDATE: I get the sense that I'm supposed to subclass RaygunClient, override Send(RaygunMessage), then create an instance and attach exception handlers. I've done this, but how do I then ensure that RaygunClient.Current refers to my custom instance?


Jason Fauchelle

Raygun

Posted on
Apr 19 2017

Hi gauche,

The original question is asking about changing the default tags after already calling the AttachExceptionHandler with an initial set of default tags. For tags specifying the current environment, I wouldn't expect you'd need to dynamically change this while the app executes.

That said, the AttachExceptionHandler method that takes tags is depricated, so I wouldn't recommend using it. Instead, you should just be able to call attachExceptionHandler(), and then at any time later, call setTags to specify the default set of tags that you want to be sent with all automatically and manually sent exceptions. No need to subclass RaygunClient.

One thing to note that you might not need to worry about is that calling setTags will overwrite all previously set default tags, so if you use this method multiple times, make sure the tags collection you set contains exactly the default tags you want to be sent with subsequently sent exception reports.

Since it's not mentioned on this thread, I also want to clarify that this is all regarding the raygun4android provider. Please let me know if you were meaning a different provider. Here is the raygun4android documentation, though it's missing the tags and custom data sections which we'll fix up: https://raygun.com/docs/languages/android

Let me know if you have further questions about this.

-Jason Fauchelle


gauche

Posted on
Apr 19 2017

OK, thanks for the reply. Let me elaborate on exactly what I'm trying to achieve.

I am running on both Xamarin Android and Xamarin iOS. I want to:

  • include a data tag for every crash report telling me the environment against which the app is running
  • include the last 100 or so lines from my log output for every crash report

Obviously the second one there is dynamic in nature.

It seems to me this should be as simple as a custom RaygunClient. Indeed, that seems to be working on Android if I include this terrible hack on startup:

var myClient = new CustomRaygunClient();
myClient.AttachCrashReporting();

// omg, no
typeof(RaygunClient)
    .GetField("_client", BindingFlags.NonPublic | BindingFlags.Static)
    .SetValue(null, myClient);

Is there a sane way to do this?


Sam

Posted on
Apr 19 2017

Hey, Not sure if this is helpful but here's what I did (Raygun might have updated since my code was written):

This will add info about appname and appid to all exceptions (you would put 'environment' instead)

RaygunClient.Init(getContext(), "RAY_GUN_KEY");

// set default tags that will be applied to ALL exceptions (even uncaught ones)
ArrayList<String> tags = new ArrayList<>();
tags.add("Uncaught Exception");
tags.add("AppName: " + getContext().getString(R.string.app_name));
tags.add("AppID: " + getContext().getString(R.string.app_id));
RaygunClient.AttachExceptionHandler(tags);

Jason Fauchelle

Raygun

Posted on
Apr 19 2017

Hi gauche,

Ignoring everything I mentioned before, the recommended way for your scenario would be to attach an event handler to the SendingMessage event of the Current RaygunClient instance.

Such an event handler will be called for all automatically and manually sent exceptions, and allows you to modify the given RaygunMessage object however you like which is then serialized and sent to Raygun. In this event handler you can set the e.Message.Details.Tags property to include the environment. The log lines might be best to go into e.Message.Details.UserCustomData.

Here is the documentation around this: https://raygun.com/docs/languages/net/xamarin-for-android#modify-cancel-message

The example in that documentation shows how to cancel sending the message, so ignore the contents of the event handler and use your own logic regarding setting tags and custom data.

Again, no need to subclass RaygunClient. I'll also point out that the Raygun provider for Xamarin does not have a concept for specifying a default set of tags like what's mentioned in the original question of this thread. So using this event handler is the only way to customize the tags of automatically sent exceptions.

Another thing to note is that this Raygun provider will automatically send a tag named "UnhandledException" with all automatically sent exceptions which is used by Raygun to distinguish between crashes vs caught exceptions. So when setting the Tags property, you may want to first check if there are existing tags and merge your tags into it, rather than overwriting it all.

Please let me know how that goes for you.

-Jason Fauchelle


Jason Fauchelle

Raygun

Posted on
Apr 19 2017

Thanks for jumping in Sam.

The Raygun provider in question though is the Xamarin.Android provider, rather than the native Android one. They both have some very similar methods, so sorry for the mix up.

-Jason Fauchelle


gauche

Posted on
Apr 19 2017

omg, how did I miss that!? That's exactly what I'm after - thanks Jason!


Reply