예제 #1
0
        internal void HandleException(Exception unhandledException)
        {
            CrashLogInformation crashInfo = this._crashLogInfo;

            //TODO refactor in next version
            if (this._crashLogInfo.PackageName == null)
            {
                this._crashLogInfo = HockeyClient.Current.AsInternal().PrefilledCrashLogInfo;
            }
            ICrashData cd = HockeyClient.Current.AsInternal().CreateCrashData(unhandledException, this._crashLogInfo);

            var crashId = Guid.NewGuid();

            try
            {
                IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
                if (!store.DirectoryExists(Constants.CrashDirectoryName))
                {
                    store.CreateDirectory(Constants.CrashDirectoryName);
                }

                String filename = string.Format("{0}{1}.log", Constants.CrashFilePrefix, crashId);
                using (FileStream stream = store.CreateFile(Path.Combine(Constants.CrashDirectoryName, filename)))
                {
                    cd.Serialize(stream);
                }
            }
            catch (Exception ex)
            {
                HockeyClient.Current.AsInternal().HandleInternalUnhandledException(ex);
            }
        }
        private void HandleException(Exception e)
        {
            try
            {
                string crashID  = Guid.NewGuid().ToString();
                String filename = String.Format("{0}{1}.log", Constants.CrashFilePrefix, crashID);

                CrashLogInformation logInfo = new CrashLogInformation()
                {
                    PackageName     = Application.Current.GetType().Namespace,
                    Version         = HockeyClient.Current.AsInternal().VersionInfo,
                    OperatingSystem = Environment.OSVersion.Platform.ToString(),
                    Windows         = Environment.OSVersion.Version.ToString() + Environment.OSVersion.ServicePack,
                    Manufacturer    = "",
                    Model           = ""
                };

                ICrashData crash = HockeyClient.Current.AsInternal().CreateCrashData(e, logInfo);
                using (FileStream stream = File.Create(Path.Combine(Constants.GetPathToHockeyCrashes(), filename)))
                {
                    crash.Serialize(stream);
                    stream.Flush();
                }
            }
            catch (Exception ex)
            {
                HockeyClient.Current.AsInternal().HandleInternalUnhandledException(ex);
            }
        }
예제 #3
0
        /// <summary>
        /// Send crash-logs from storage and deletes the if they could be sent
        /// </summary>
        /// <returns>true if at least one Crashlog was transmitted to the server</returns>
        public async Task <bool> SendCrashesAndDeleteAfterwardsAsync()
        {
            bool atLeatOneCrashSent = false;

#if NET_4_5
            using (var releaser = await lck.LockAsync()) {
#else
            if (Monitor.TryEnter(this))
            {
                try {
#endif
                logger.Info("Start send crashes to platform.");
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    foreach (string filename in await this.GetCrashFileNamesAsync())
                    {
                        logger.Info("Crashfile found: {0}", filename);
                        Exception error = null;
                        try                                 //don't stop if one file fails
                        {
                            using (var stream = await this.PlatformHelper.GetStreamAsync(filename, SDKConstants.CrashDirectoryName)) {
                                ICrashData cd = this.Deserialize(stream);
                                await cd.SendDataAsync();
                            }

                            atLeatOneCrashSent = true;
                        } catch (Exception ex) {
                            HandleInternalUnhandledException(ex);
                            error = ex;
                        }
                        if (error != null && error is WebTransferException)
                        {
                            //will retry on next start
                        }
                        else
                        {
                            //either no error or the file seems corrupt => try to delete it
                            try {
                                await this.PlatformHelper.DeleteFileAsync(filename, SDKConstants.CrashDirectoryName);
                            } catch (Exception ex) {
                                HandleInternalUnhandledException(ex);
                            }
                        }
                    }
                }
            }
#if !NET_4_5
            finally {
                try {
                    Monitor.Exit(this);
                } catch (Exception ex) {                         //ignore. on next start it will try again.
                    HandleInternalUnhandledException(ex);
                }
            }
        }
#endif
            return(atLeatOneCrashSent);
        }
        private void ExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            // check if we are allowed to handle exceptions
            if (_disposed)
            {
                return;
            }

            // the exception handler as self should never throw an exception so that the flow is
            // not interrupted by some bad code here
            try
            {
                // We need to ensure that the crash file location realy exists before we start
                // writing a file into it
                if (!Directory.Exists(_crashFileLocaton))
                {
                    Directory.CreateDirectory(_crashFileLocaton);
                }

                // Every exception needs a unique identifier
                string crashID = Guid.NewGuid().ToString();

                // The filename is generated by the identifier
                String filename = String.Format("{0}.log", crashID);

                // generate the full filename
                filename = Path.Combine(_crashFileLocaton, filename);

                // Generate the model HockeyApp is using for logging crashes
                CrashLogInformation logInfo = new CrashLogInformation()
                {
                    PackageName     = _packageName,
                    Version         = _packageVersion,
                    OperatingSystem = Environment.OSVersion.ToString(),
                };


                // Now it's time to build a real creash report and write them to the file
                ICrashData crash = HockeyClient.Instance.CreateCrashData(e.ExceptionObject as Exception, logInfo);
                using (FileStream stream = File.Create(filename))
                {
                    crash.Serialize(stream);
                }
            }
            catch (Exception)
            {
                // Ignore all exceptions
            }
        }
예제 #5
0
        /// <summary>
        /// Handle exception asyncronously
        /// </summary>
        /// <param name="ex">the exception that should be saved to a crashlog</param>
        public async Task HandleExceptionAsync(Exception ex)
        {
            ICrashData cd = this.CreateCrashData(ex);

            var crashId = Guid.NewGuid();

            try {
                using (MemoryStream stream = new MemoryStream()) {
                    cd.Serialize(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    await this.PlatformHelper.WriteStreamToFileAsync(stream, string.Format("{0}{1}.log", SDKConstants.CrashFilePrefix, crashId), SDKConstants.CrashDirectoryName);
                }
            } catch (Exception e) {
                HandleInternalUnhandledException(e);
            }
        }
예제 #6
0
        public CrashHandler()
        {
            HockeyApp.HockeyClient.Configure(this._appID, Assembly.GetExecutingAssembly().GetName().Version.ToString());

            this.crashFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            this.crashFilePath = Path.Combine(this.crashFilePath, "Hoch");

            AppDomain.CurrentDomain.UnhandledException += (a, b) =>
            {
                string crashID  = Guid.NewGuid().ToString();
                String filename = String.Format("{0}{1}.log", _crashFilePrefix, crashID);

                CrashLogInformation logInfo = new CrashLogInformation()
                {
                    PackageName     = "HockeyUploaderConsole",
                    Version         = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                    OperatingSystem = Environment.OSVersion.ToString(),
                };

                ICrashData crash  = HockeyClient.Instance.CreateCrashData(b.ExceptionObject as Exception, logInfo);
                FileStream stream = File.Create(Path.Combine(this._crashFilePrefix, filename));
                crash.Serialize(stream);
                stream.Flush();
                stream.Close();
            };

            if (Directory.Exists(this.crashFilePath))
            {
                foreach (string filename in Directory.GetFiles(this.crashFilePath, _crashFilePrefix + "*.log"))
                {
                    try
                    {
                        FileStream fs = File.OpenRead(filename);
                        ICrashData cd = HockeyClient.Instance.Deserialize(fs);
                        fs.Close();
                        cd.SendDataAsync().Wait();
                        File.Delete(filename);
                    }
                    catch (Exception ex)
                    {
                        Program.LogToConsole("Error sending crash-information: " + ex.Message);
                    }
                }
            }
        }
예제 #7
0
        public void HandleException(Exception ex)
        {
            if (!this.PlatformHelper.PlatformSupportsSyncWrite)
            {
                throw new Exception("PlatformHelper implementation error.");
            }
            ICrashData cd      = this.CreateCrashData(ex);
            var        crashId = Guid.NewGuid();

            try {
                using (MemoryStream stream = new MemoryStream()) {
                    cd.Serialize(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    this.PlatformHelper.WriteStreamToFileSync(stream, string.Format("{0}{1}.log", SDKConstants.CrashFilePrefix, crashId), SDKConstants.CrashDirectoryName);
                }
            } catch (Exception e) {
                HandleInternalUnhandledException(e);
            }
        }
예제 #8
0
        internal async Task SendCrashesNowAsync()
        {
            bool deleteFlag = false; //necessary, because no await allowed in catch body

            logger.Info("Start send crashes to platform.");

            if (NetworkInterface.GetIsNetworkAvailable())
            {
                foreach (StorageFile crashFile in await this.GetCrashFiles())
                {
                    logger.Info("Crashfile found: {0}", crashFile.Name);
                    deleteFlag = false;
                    try
                    {
                        Stream fs = await crashFile.OpenStreamForReadAsync();

                        ICrashData cd = ((HockeyClient)HockeyClient.Current).Deserialize(fs);
                        fs.Dispose();
                        await cd.SendDataAsync();

                        await crashFile.DeleteAsync();

                        logger.Info("Crashfile deleted: {0}", crashFile.Name);
                    }
                    catch (WebTransferException ex)
                    {
                        this.logger.Error(ex);
                    }
                    catch (Exception ex)
                    {
                        this.logger.Error(ex);
                        deleteFlag = true;
                    }
                    if (deleteFlag)
                    {
                        await crashFile.DeleteAsync();
                    }
                }
            }
        }
        private Boolean TryPushCrashLogs(out List <String> failedReports)
        {
            // reset the failed reports
            failedReports = null;

            // check if we have a crash file directory, if not nothing todo
            if (!Directory.Exists(_crashFileLocaton))
            {
                return(true);
            }

            //
            // Visit every file and send to the backend
            foreach (string filename in Directory.GetFiles(_crashFileLocaton, "*.log"))
            {
                try
                {
                    FileStream fs = File.OpenRead(filename);
                    ICrashData cd = HockeyClient.Instance.Deserialize(fs);
                    fs.Close();
                    cd.SendDataAsync().Wait();
                    File.Delete(filename);
                }
                catch (Exception)
                {
                    // create a failed report list if needed
                    if (failedReports == null)
                    {
                        failedReports = new List <string>();
                    }

                    // add the missing report
                    failedReports.Add(filename);
                }
            }

            // done
            return(failedReports == null);
        }
        internal async Task SendCrashesNowAsync()
        {
            //System Semaphore would be another possibility. But the worst thing that can happen now, is
            //that a crash is send twice.
            if (!System.Threading.Monitor.TryEnter(this))
            {
                logger.Warn("Sending crashes was called multiple times!");
                throw new Exception("Hockey is already sending crashes to server!");
            }
            else
            {
                logger.Info("Start send crashes to platform.");

                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    foreach (string crashFileName in this.GetCrashFiles())
                    {
                        logger.Info("Crashfile found: {0}", crashFileName);
                        try
                        {
                            using (FileStream fs = File.Open(crashFileName, FileMode.Open, FileAccess.ReadWrite))
                            {
                                ICrashData cd = HockeyClient.Current.AsInternal().Deserialize(fs);
                                await cd.SendDataAsync();
                            }
                            //if the process switch occurs between those lines the worst that can happen is that a crash is sent twice.
                            File.Delete(crashFileName);
                            logger.Info("Crashfile sent and deleted: {0}", crashFileName);
                        }
                        catch (Exception ex)
                        {
                            HockeyClient.Current.AsInternal().HandleInternalUnhandledException(ex);
                        }
                    }
                }
            }
            System.Threading.Monitor.Exit(this);
        }