I know this is quite and old thread but maybe it helps for some who have the same issue.
I had the problem that Sentry got flooded with thousands of null ref exceptions in my Unity project within one session and that could eat up any quota pretty quickly.
Here's what I did for Unity:
using UnityEngine;
using Sentry.Unity;
using System.Collections.Generic;
[CreateAssetMenu(fileName = "Assets/Resources/Sentry/SentryRuntimeConfiguration.asset", menuName = "Sentry/SentryRuntimeConfiguration", order = 999)]
public class SentryRuntimeConfiguration : Sentry.Unity.SentryRuntimeOptionsConfiguration
{
private bool _userIdLogged = false;
private HashSet<string> _loggedMessages = new HashSet<string>();
/// Called at the player startup by SentryInitialization.
/// You can alter configuration for the C# error handling and also
/// native error handling in platforms **other** than iOS, macOS and Android.
/// Learn more at https://docs.sentry.io/platforms/unity/configuration/options/#programmatic-configuration
public override void Configure(SentryUnityOptions options)
{
// this option will filter the sending of data to Sentry in case the user did not give consent
options.SetBeforeSend((sentryEvent, hint) =>
{
bool sendAnalytics = PlayerPrefs.GetInt("SendAnalytics") == 1;
if (!sendAnalytics)
{
return null; // Don't send this event to Sentry
}
if (!_userIdLogged)
{
var userID = sentryEvent.User?.Id;
if (userID != null)
{
// I'm logging the user ID here
}
_userIdLogged = true;
}
var stackTrace = sentryEvent.Exception?.StackTrace;
if (stackTrace != null)
{
if (_loggedMessages.Contains(stackTrace))
{
// we already had this issue tracked, don't track the same exception over and over again
return null;
}
// this one is new, but remember it for this session
_loggedMessages.Add(stackTrace);
}
return sentryEvent;
});
}
}