Beispiel #1
0
        /// <summary>
        /// Initializes Sentry Unity SDK while configuring the options.
        /// </summary>
        /// <param name="sentryUnityOptionsConfigure">Callback to configure the options.</param>
        public static void Init(Action <SentryUnityOptions> sentryUnityOptionsConfigure)
        {
            var options = new SentryUnityOptions();

            sentryUnityOptionsConfigure.Invoke(options);
            Init(options);
        }
        // Call after native init() to check if the application has crashed in the previous run and clear the status.
        // Because the file is removed, the result will change on subsequent calls so it must be cached for the current runtime.
        internal static bool HandleCrashedLastRun(SentryUnityOptions options)
        {
            var result = sentry_get_crashed_last_run() == 1;

            sentry_clear_crashed_last_run();
            return(result);
        }
        public static void Init(SentryUnityOptions options)
        {
            var cOptions = sentry_options_new();

            // Note: DSN is not null because options.IsValid() must have returned true for this to be called.
            sentry_options_set_dsn(cOptions, options.Dsn !);

            if (options.Release is not null)
            {
                options.DiagnosticLogger?.LogDebug("Setting Release: {0}", options.Release);
                sentry_options_set_release(cOptions, options.Release);
            }

            if (options.Environment is not null)
            {
                options.DiagnosticLogger?.LogDebug("Setting Environment: {0}", options.Environment);
                sentry_options_set_environment(cOptions, options.Environment);
            }

            options.DiagnosticLogger?.LogDebug("Setting Debug: {0}", options.Debug);
            sentry_options_set_debug(cOptions, options.Debug ? 1 : 0);

            if (options.SampleRate.HasValue)
            {
                options.DiagnosticLogger?.LogDebug("Setting Sample Rate: {0}", options.SampleRate.Value);
                sentry_options_set_sample_rate(cOptions, options.SampleRate.Value);
            }

            // Disabling the native in favor of the C# layer for now
            options.DiagnosticLogger?.LogDebug("Disabling native auto session tracking");
            sentry_options_set_auto_session_tracking(cOptions, 0);

            var dir = GetCacheDirectory(options);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                options.DiagnosticLogger?.LogDebug("Setting CacheDirectoryPath on Windows: {0}", dir);
                sentry_options_set_database_pathw(cOptions, dir);
            }
            else
            {
                options.DiagnosticLogger?.LogDebug("Setting CacheDirectoryPath: {0}", dir);
                sentry_options_set_database_path(cOptions, dir);
            }

            if (options.DiagnosticLogger is null)
            {
                _logger?.LogDebug("Unsetting the current native logger");
                _logger = null;
            }
            else
            {
                options.DiagnosticLogger.LogDebug($"{(_logger is null ? "Setting a" : "Replacing the")} native logger");
                _logger = options.DiagnosticLogger;
                sentry_options_set_logger(cOptions, new sentry_logger_function_t(nativeLog), IntPtr.Zero);
            }

            sentry_init(cOptions);
        }
        internal static SentryUnityOptions ToSentryUnityOptions(ScriptableSentryUnityOptions scriptableOptions, bool isBuilding, IApplication?application = null)
        {
            application ??= ApplicationAdapter.Instance;

            var options = new SentryUnityOptions(application, isBuilding)
            {
                Enabled                     = scriptableOptions.Enabled,
                Dsn                         = scriptableOptions.Dsn,
                CaptureInEditor             = scriptableOptions.CaptureInEditor,
                EnableLogDebouncing         = scriptableOptions.EnableLogDebouncing,
                TracesSampleRate            = scriptableOptions.TracesSampleRate,
                AutoSessionTracking         = scriptableOptions.AutoSessionTracking,
                AutoSessionTrackingInterval = TimeSpan.FromMilliseconds(scriptableOptions.AutoSessionTrackingInterval),
                AttachStacktrace            = scriptableOptions.AttachStacktrace,
                MaxBreadcrumbs              = scriptableOptions.MaxBreadcrumbs,
                ReportAssembliesMode        = scriptableOptions.ReportAssembliesMode,
                SendDefaultPii              = scriptableOptions.SendDefaultPii,
                IsEnvironmentUser           = scriptableOptions.IsEnvironmentUser,
                MaxCacheItems               = scriptableOptions.MaxCacheItems,
                InitCacheFlushTimeout       = TimeSpan.FromMilliseconds(scriptableOptions.InitCacheFlushTimeout),
                SampleRate                  = scriptableOptions.SampleRate,
                ShutdownTimeout             = TimeSpan.FromMilliseconds(scriptableOptions.ShutdownTimeout),
                MaxQueueItems               = scriptableOptions.MaxQueueItems
            };

            if (!string.IsNullOrWhiteSpace(scriptableOptions.ReleaseOverride))
            {
                options.Release = scriptableOptions.ReleaseOverride;
            }

            if (!string.IsNullOrWhiteSpace(scriptableOptions.EnvironmentOverride))
            {
                options.Environment = scriptableOptions.EnvironmentOverride;
            }

            if (!scriptableOptions.EnableOfflineCaching)
            {
                options.CacheDirectoryPath = null;
            }

            options.IosNativeSupportEnabled     = scriptableOptions.IosNativeSupportEnabled;
            options.AndroidNativeSupportEnabled = scriptableOptions.AndroidNativeSupportEnabled;
            options.WindowsNativeSupportEnabled = scriptableOptions.WindowsNativeSupportEnabled;

            options.Debug             = scriptableOptions.Debug;
            options.DebugOnlyInEditor = scriptableOptions.DebugOnlyInEditor;
            options.DiagnosticLevel   = scriptableOptions.DiagnosticLevel;

            SentryOptionsUtility.TryAttachLogger(options);

            scriptableOptions.OptionsConfiguration?.Configure(options);

            return(options);
        }
        public static void TryAttachLogger(SentryUnityOptions options, IApplication?application = null)
        {
            application ??= ApplicationAdapter.Instance;

            if (options.DiagnosticLogger is null &&
                options.Debug &&
                (!options.DebugOnlyInEditor || application.IsEditor))
            {
                options.DiagnosticLogger = new UnityLogger(options);
            }
        }
 internal static string GetCacheDirectory(SentryUnityOptions options)
 {
     if (options.CacheDirectoryPath is null)
     {
         // same as the default of sentry-native
         return(Path.Combine(Directory.GetCurrentDirectory(), ".sentry-native"));
     }
     else
     {
         return(Path.Combine(options.CacheDirectoryPath, "SentryNative"));
     }
 }
Beispiel #7
0
        public static void Init(SentryUnityOptions options)
        {
            SentryOptionsUtility.TryAttachLogger(options);
            if (options.ShouldInitializeSdk())
            {
                // On Standalone, we disable cache dir in case multiple app instances run over the same path.
                // Note: we cannot use a named Mutex, because Unit doesn't support it. Instead, we create a file with `FileShare.None`.
                // https://forum.unity.com/threads/unsupported-internal-call-for-il2cpp-mutex-createmutex_internal-named-mutexes-are-not-supported.387334/
                if (ApplicationAdapter.Instance.Platform is RuntimePlatform.WindowsPlayer && options.CacheDirectoryPath is not null)
                {
                    try
                    {
                        _lockFile = new FileStream(Path.Combine(options.CacheDirectoryPath, "sentry-unity.lock"), FileMode.OpenOrCreate,
                                                   FileAccess.ReadWrite, FileShare.None);
                    }
                    catch (Exception ex)
                    {
                        options.DiagnosticLogger?.LogWarning("An exception was thrown while trying to " +
                                                             "acquire a lockfile on the config directory: .NET event cache will be disabled.", ex);
                        options.CacheDirectoryPath  = null;
                        options.AutoSessionTracking = false;
                    }
                }

                var sentryDotNet = SentrySdk.Init(options);
                ApplicationAdapter.Instance.Quitting += () =>
                {
                    options.DiagnosticLogger?.LogDebug("Closing the sentry-dotnet SDK");
                    try
                    {
                        sentryDotNet.Dispose();
                    }
                    finally
                    {
                        try
                        {
                            // We don't really need to close, Windows would release the lock anyway, but let's be nice.
                            _lockFile?.Close();
                        }
                        catch (Exception ex)
                        {
                            options.DiagnosticLogger?.Log(SentryLevel.Warning,
                                                          "Exception while releasing the lockfile on the config directory.", ex);
                        }
                    }
                };
            }
        }
        public static void SetDefaults(ScriptableSentryUnityOptions scriptableOptions)
        {
            var options = new SentryUnityOptions();

            scriptableOptions.Enabled = options.Enabled;

            scriptableOptions.Dsn                         = options.Dsn;
            scriptableOptions.CaptureInEditor             = options.CaptureInEditor;
            scriptableOptions.TracesSampleRate            = options.TracesSampleRate;
            scriptableOptions.AutoSessionTrackingInterval = (int)options.AutoSessionTrackingInterval.TotalMilliseconds;
            scriptableOptions.AutoSessionTracking         = options.AutoSessionTracking;

            scriptableOptions.AttachStacktrace     = options.AttachStacktrace;
            scriptableOptions.MaxBreadcrumbs       = options.MaxBreadcrumbs;
            scriptableOptions.ReportAssembliesMode = options.ReportAssembliesMode;
            scriptableOptions.SendDefaultPii       = options.SendDefaultPii;
            scriptableOptions.IsEnvironmentUser    = options.IsEnvironmentUser;

            scriptableOptions.MaxCacheItems         = options.MaxCacheItems;
            scriptableOptions.InitCacheFlushTimeout = (int)options.InitCacheFlushTimeout.TotalMilliseconds;
            scriptableOptions.SampleRate            = options.SampleRate;
            scriptableOptions.ShutdownTimeout       = (int)options.ShutdownTimeout.TotalMilliseconds;
            scriptableOptions.MaxQueueItems         = options.MaxQueueItems;

            // Config window specifics
            scriptableOptions.ReleaseOverride     = string.Empty;
            scriptableOptions.EnvironmentOverride = string.Empty;

            scriptableOptions.EnableOfflineCaching = true;

            scriptableOptions.IosNativeSupportEnabled     = options.IosNativeSupportEnabled;
            scriptableOptions.AndroidNativeSupportEnabled = options.AndroidNativeSupportEnabled;
            scriptableOptions.WindowsNativeSupportEnabled = options.WindowsNativeSupportEnabled;

            scriptableOptions.Debug             = true;
            scriptableOptions.DebugOnlyInEditor = true;
            scriptableOptions.DiagnosticLevel   = SentryLevel.Warning;
        }
 public abstract void Configure(SentryUnityOptions options);
Beispiel #10
0
 public UnityLogger(SentryUnityOptions sentryUnityOptions) : this(sentryUnityOptions, null)
 {
 }