/// <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); }
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); } } } }
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); }