Exemplo n.º 1
0
    void OnEnable()
    {
        Backtrace.Unity.BacktraceClient backtraceClient = FindObjectOfType <Backtrace.Unity.BacktraceClient>();
        Assert.IsNotNull(backtraceClient);

        backtraceClient.BeforeSend += AddAttributes;

        Debug.Log("Added BeforeSend handler");
    }
    void Start()
    {
        string serverUrl   = "";
        string accessToken = "";

        try
        {
            using (System.IO.StreamReader configFile = new System.IO.StreamReader(ConfigFileName))
            {
                serverUrl   = configFile.ReadLine();
                accessToken = configFile.ReadLine();
            }
        }
        catch (System.Exception e)
        {
            Debug.LogErrorFormat("Unable to read from config file {0}: {1}", ConfigFileName, e);
            return;
        }

        Backtrace.Unity.Model.BacktraceConfiguration backtraceConfiguration = ScriptableObject.CreateInstance <Backtrace.Unity.Model.BacktraceConfiguration>();

        backtraceConfiguration.ServerUrl = serverUrl;
        backtraceConfiguration.Token     = accessToken;

        backtraceConfiguration.HandleUnhandledExceptions = true;

        backtraceConfiguration.Enabled        = false;
        backtraceConfiguration.CreateDatabase = false;
        backtraceConfiguration.AutoSendMode   = true;

        backtraceConfiguration.ReportPerMin = 0;
        backtraceConfiguration.RetryLimit   = 3;
        backtraceConfiguration.RetryOrder   = Backtrace.Unity.Types.RetryOrder.Stack;

        gameObject.SetActive(false);
        Backtrace.Unity.BacktraceClient backtraceClient = gameObject.AddComponent <Backtrace.Unity.BacktraceClient>();
        backtraceClient.Configuration = backtraceConfiguration;
        gameObject.SetActive(true);

        Debug.LogFormat("Added Backtrace client with serverUrl = {0} and token = {1}", serverUrl, accessToken);
    }
        public void Refresh()
        {
            if (Configuration == null || !Configuration.IsValid())
            {
                return;
            }

            if (Instance != null)
            {
                return;
            }

            Enabled  = true;
            _current = Thread.CurrentThread;
            CaptureUnityMessages();
            _reportLimitWatcher      = new ReportLimitWatcher(Convert.ToUInt32(Configuration.ReportPerMin));
            _clientReportAttachments = Configuration.GetAttachmentPaths();

            BacktraceApi = new BacktraceApi(
                credentials: new BacktraceCredentials(Configuration.GetValidServerUrl()),

#if UNITY_2018_4_OR_NEWER
                ignoreSslValidation: Configuration.IgnoreSslValidation
#else
                ignoreSslValidation: false
#endif
                );
            BacktraceApi.EnablePerformanceStatistics = Configuration.PerformanceStatistics;

            if (!Configuration.DestroyOnLoad)
            {
                DontDestroyOnLoad(gameObject);
                _instance = this;
            }

#if !UNITY_WEBGL
            EnableMetrics(false);
#endif
            var nativeAttachments = _clientReportAttachments.ToList()
                                    .Where(n => !string.IsNullOrEmpty(n))
                                    .OrderBy(System.IO.Path.GetFileName, StringComparer.InvariantCultureIgnoreCase).ToList();

            if (Configuration.Enabled)
            {
                Database = GetComponent <BacktraceDatabase>();
                if (Database != null)
                {
                    Database.Reload();
                    Database.SetApi(BacktraceApi);
                    Database.SetReportWatcher(_reportLimitWatcher);
                    if (Database.Breadcrumbs != null)
                    {
                        nativeAttachments.Add(Database.Breadcrumbs.GetBreadcrumbLogPath());
                    }
                    _nativeClient = NativeClientFactory.CreateNativeClient(Configuration, name, AttributeProvider.GenerateAttributes(false), nativeAttachments);
                    Database.EnableBreadcrumbsSupport();
                }
            }

            AttributeProvider.AddDynamicAttributeProvider(_nativeClient);

            if (Configuration.SendUnhandledGameCrashesOnGameStartup && isActiveAndEnabled)
            {
                var nativeCrashUplaoder = new NativeCrashUploader(AttributeProvider, BacktraceApi);
                StartCoroutine(nativeCrashUplaoder.SendUnhandledGameCrashesOnGameStartup());
            }
        }