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);
        }
Exemplo n.º 2
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);
                        }
                    }
                };
            }
        }