Custom error grouping for .NET Exceptions

| 2 min. (385 words)

Raygun tries its hardest to group your exceptions together intelligently, but it’s a hard problem to solve and we don’t always get it right for everyone. To help with this, we’ve added the ability to define your own grouping hash for .NET exceptions before you send them.

What is a group hash?

It’s the key that tells Raygun which error group the error should go into. Generally the payload hits the Raygun servers, which runs a hashing algorithm over it and sorts it into the correct group. This works 99.99% of the time – your errors show up exactly where you expect them and everybody is happy.

Of course there is always the chance that Raygun’s hasher doesn’t group your error how you desire.

What are my options?

We have two options to get around this problem:

  1. Use message based hashing, but this has limitations the second you have two messages that are the same but have very different causes.
  2. Now you can also provide your own hashing key!

What does providing your own hashing key involve?

It’s quite easy. You attach an event handler to RaygunClient, which gives you the Exception and the RaygunMessage that Raygun4NET has created, and you send back the key you want to use for grouping that error. If you don’t set a grouping key, we use our normal hashing routine.

Here’s the basic idea:

var raygunClient = new RaygunClient();

raygun.CustomGroupingKey += (sender, e) => {
  if(e.Exception is SqlException)
  {
    e.CustomGroupingKey = "SqlException" + ((SqlException)e.Exception).Number;
  }
};

This will set the custom grouping key for any SqlException to be the string “SqlException” plus the error number of the underlying SQL error, so any SqlExceptions will be grouped together as long as they have the same error number. Not only that, but other exceptions will still be grouped normally by Raygun. Great!

The grouping key you produce should probably be less than 100 characters – we don’t actually restrict you here, but if it’s longer than that we will SHA1 hash your key and use the results of that. It’s quite unlikely that this will produce duplicates, but it’s worth mentioning.

WARNING – Custom error hashing should be a last resort for when the built-in hashers don’t quite suit you – but as always, the more choices you have the better!