feature<Issue>: always generate crash log file instead of report to gitee.com

This commit is contained in:
leo 2021-11-09 10:21:31 +08:00
parent 9a4fde74b2
commit b452456d9d
3 changed files with 20 additions and 71 deletions

View file

@ -1,70 +1,31 @@
using System;
using System.Reflection;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
#if NET6_0_OR_GREATER
using System.Net.Http;
#else
using System.Net;
#endif
using System.Diagnostics;
namespace SourceGit.Models {
/// <summary>
/// 崩溃日志上报
/// 崩溃日志生成
/// </summary>
public class Issue {
[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; }
/// <summary>
/// 创建Gitee平台ISSUE
/// </summary>
/// <param name="e"></param>
public static void Create(System.Exception e) {
if (!Preference.Instance.General.EnableCrashReport) return;
var builder = new StringBuilder();
builder.Append("Crash: ");
builder.Append(e.Message);
builder.Append("\n\n");
builder.Append("----------------------------\n");
builder.Append($"Windows OS: {Environment.OSVersion}\n");
builder.Append($"Version: {Assembly.GetExecutingAssembly().GetName().Version}");
builder.Append($"Platform: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}");
builder.Append($"Source: {e.Source}");
builder.Append($"---------------------------\n\n");
builder.Append(e.StackTrace);
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);
#if NET6_0_OR_GREATER
var content = new StringContent(JsonSerializer.Serialize(issue), Encoding.UTF8, "application/json");
var req = new HttpClient() { Timeout = TimeSpan.FromSeconds(1) };
req.PostAsync("https://gitee.com/api/v5/repos/sourcegit/issues", content).Wait();
#else
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()) {
var data = JsonSerializer.Serialize(issue);
var raw = Encoding.UTF8.GetBytes(data);
writer.Write(raw, 0, raw.Length);
}
req.GetResponse();
#endif
} catch { }
var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
var file = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
file = Path.Combine(file, $"sourcegit_crash_{time}.log");
File.WriteAllText(file, builder.ToString());
}
}
}

View file

@ -68,11 +68,6 @@ namespace SourceGit.Models {
/// </summary>
public int LastCheckDay { get; set; } = 0;
/// <summary>
/// 是否启用崩溃上报
/// </summary>
public bool EnableCrashReport { get; set; } = false;
/// <summary>
/// 是否尝试使用 Windows Terminal 打开终端
/// </summary>

View file

@ -12,7 +12,7 @@
WindowStartupLocation="CenterOwner"
ResizeMode="NoResize"
Title="{DynamicResource Text.Preference}"
Width="500" Height="340">
Width="500" Height="312">
<Grid>
<Grid.Resources>
<converters:FontFamiliesToName x:Key="FontFamiliesToName"/>
@ -68,7 +68,6 @@
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
@ -168,15 +167,9 @@
Content="{DynamicResource Text.Preference.RestoreTabs}"
IsChecked="{Binding Source={x:Static models:Preference.Instance}, Path=Restore.IsEnabled, Mode=TwoWay}"/>
<!-- Crash Report -->
<CheckBox
Grid.Row="7" Grid.Column="1"
Content="{DynamicResource Text.Preference.EnableCrashReport}"
IsChecked="{Binding Source={x:Static models:Preference.Instance}, Path=General.EnableCrashReport, Mode=TwoWay}"/>
<!-- Use Windows Terminal -->
<CheckBox
Grid.Row="8" Grid.Column="1"
Grid.Row="7" Grid.Column="1"
Content="{DynamicResource Text.Preference.UseWindowsTerminal}"
IsEnabled="{Binding ElementName=me, Path=HasWindowsTerminal}"
IsChecked="{Binding Source={x:Static models:Preference.Instance}, Path=General.UseWindowsTerminal, Mode=TwoWay}"/>