feature<Issue>: create Gitee issue when unhandled exception raised

This commit is contained in:
leo 2021-08-03 10:19:30 +08:00
parent 89c0f0077e
commit a56a3ac5d2
3 changed files with 91 additions and 5 deletions

View file

@ -1,8 +1,6 @@
<Application x:Class="SourceGit.App" <Application x:Class="SourceGit.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Startup="OnAppStartup"
Deactivated="OnAppDeactivated">
<Application.Resources> <Application.Resources>
<ResourceDictionary> <ResourceDictionary>
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace SourceGit { namespace SourceGit {
@ -26,7 +27,12 @@ namespace SourceGit {
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
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)) { if (!Directory.Exists(Views.Controls.Avatar.CACHE_PATH)) {
Directory.CreateDirectory(Views.Controls.Avatar.CACHE_PATH); Directory.CreateDirectory(Views.Controls.Avatar.CACHE_PATH);
@ -79,7 +85,8 @@ namespace SourceGit {
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void OnAppDeactivated(object sender, EventArgs e) { protected override void OnDeactivated(EventArgs e) {
base.OnDeactivated(e);
GC.Collect(); GC.Collect();
Models.Preference.Save(); Models.Preference.Save();
} }

81
src/Models/Issue.cs Normal file
View file

@ -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 {
/// <summary>
/// 崩溃日志上报
/// </summary>
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
/// <summary>
/// 创建Gitee平台ISSUE
/// </summary>
/// <param name="e"></param>
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 {}
}
}
}