diff --git a/src/App.xaml b/src/App.xaml index 3eb35fa1..bd7132ed 100644 --- a/src/App.xaml +++ b/src/App.xaml @@ -1,8 +1,6 @@ + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> diff --git a/src/App.xaml.cs b/src/App.xaml.cs index 1e12f2b4..f62a01fc 100644 --- a/src/App.xaml.cs +++ b/src/App.xaml.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using System.Windows; namespace SourceGit { @@ -26,7 +27,12 @@ namespace SourceGit { /// /// /// - private void OnAppStartup(object sender, StartupEventArgs e) { + protected override void OnStartup(StartupEventArgs e) { + base.OnStartup(e); + + // 崩溃上报 + AppDomain.CurrentDomain.UnhandledException += (_, ev) => Models.Issue.Create(ev.ExceptionObject as Exception); + // 创建必要目录 if (!Directory.Exists(Views.Controls.Avatar.CACHE_PATH)) { Directory.CreateDirectory(Views.Controls.Avatar.CACHE_PATH); @@ -79,7 +85,8 @@ namespace SourceGit { /// /// /// - private void OnAppDeactivated(object sender, EventArgs e) { + protected override void OnDeactivated(EventArgs e) { + base.OnDeactivated(e); GC.Collect(); Models.Preference.Save(); } diff --git a/src/Models/Issue.cs b/src/Models/Issue.cs new file mode 100644 index 00000000..834e1f6c --- /dev/null +++ b/src/Models/Issue.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.Reflection; + +#if NET48 +using Newtonsoft.Json; +#else +using System.Text.Json; +using System.Text.Json.Serialization; +#endif + +namespace SourceGit.Models { + /// + /// 崩溃日志上报 + /// + public class Issue { +#if NET48 + [JsonProperty(PropertyName = "access_token")] + public string AccessToken { get; set; } + [JsonProperty(PropertyName = "repo")] + public string Repo { get; set; } + [JsonProperty(PropertyName = "title")] + public string Title { get; set; } + [JsonProperty(PropertyName = "body")] + public string Body { get; set; } +#else + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + [JsonPropertyName("repo")] + public string Repo { get; set; } + [JsonPropertyName("title")] + public string Title { get; set; } + [JsonPropertyName("body")] + public string Body { get; set; } +#endif + + /// + /// 创建Gitee平台ISSUE + /// + /// + public static void Create(System.Exception e) { + try { + var issue = new Issue(); + issue.AccessToken = "d0d56410f13a3826b87fb0868d5a26ce"; // 这是我个人的Token,仅启用ISSUE创建功能,请不要使用 + issue.Repo = "sourcegit"; + issue.Title = "CrashReport: " + e.Message; + issue.Body = string.Format( + "{0}\n\n**Base Information:**\n\n| Windows OS | {1} |\n|---|---|\n| Version | {2} |\n| Platform | {3} |\n\n**Source:** {4}\n\n**StackTrace:**\n\n ```\n{5}\n```\n", + e.Message, + Environment.OSVersion.ToString(), + Assembly.GetExecutingAssembly().GetName().Version.ToString(), + AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName, + e.Source, + e.StackTrace); + + var req = WebRequest.CreateHttp("https://gitee.com/api/v5/repos/sourcegit/issues"); + req.Method = "POST"; + req.ContentType = "application/json"; + req.Headers.Add("charset", "UTF-8"); + req.Timeout = 1000; + + using (var writer = req.GetRequestStream()) { +#if NET48 + var data = JsonConvert.SerializeObject(issue); + var raw = Encoding.UTF8.GetBytes(data); + writer.Write(raw, 0, raw.Length); +#else + var data = JsonSerializer.Serialize(issue); + writer.Write(Encoding.UTF8.GetBytes(data)); +#endif + } + + req.GetResponse(); + } catch {} + } + } +}