-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReportBugForm.cs
110 lines (100 loc) · 3.86 KB
/
ReportBugForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Specialized;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace MsCommon.ClickOnce
{
public partial class ReportBugForm : AppForm
{
public static string ReportBugEndpoint { get; set; }
private NameValueCollection _collectedData;
public ReportBugForm(Exception ex)
{
InitializeComponent();
_collectedData = CollectData(ex);
tbReport.Text = string.Join("\r\n", _collectedData.Keys.Cast<string>().Select(k => k + ": " + _collectedData[k]).ToArray());
this.AcceptButton = btnYes;
this.CancelButton = btnNo;
if (Debugger.IsAttached)
Debugger.Break();
}
private void HandleFormLoad(object sender, EventArgs e)
{
UpdateStatus(Color.Black, "");
BringToFront();
btnYes.Focus();
}
private void ButtonState(bool enabled)
{
btnYes.Enabled = enabled;
btnNo.Enabled = enabled;
tbCustomMessage.Enabled = enabled;
}
private async void HandleYesClicked(object sender, EventArgs e)
{
ButtonState(false);
_collectedData["custommessage"] = tbCustomMessage.Text;
UpdateStatus(Color.Blue, "Submitting...");
using (var client = new WebClient())
{
try
{
byte[] response = await client.UploadValuesTaskAsync(ReportBugEndpoint, "POST", _collectedData);
string responseStr = Encoding.ASCII.GetString(response);
if (responseStr.Equals("ok"))
{
UpdateStatus(Color.Green, "Submitted, thank you!");
}
else
{
UpdateStatus(Color.Red, "Server error: " + responseStr);
ButtonState(true);
}
}
catch (Exception uploadex)
{
UpdateStatus(Color.Red, "Failed to submit: " + uploadex.Message);
ButtonState(true);
}
}
}
private void HandleNoClicked(object sender, EventArgs e)
{
Close();
}
private void UpdateStatus(Color c, string status)
{
if (lblStatus.InvokeRequired)
{
lblStatus.Invoke((Action<Color, string>)UpdateStatus, c, status);
return;
}
lblStatus.Font = new Font(Label.DefaultFont, FontStyle.Bold);
lblStatus.ForeColor = c;
lblStatus.Text = status;
}
protected NameValueCollection CollectData(Exception ex)
{
var data = new NameValueCollection();
data["appname"] = AppVersion.AppName;
data["appversion"] = AppVersion.GetVersion();
data["errormessage"] = ex.Message ?? "no exception message";
data["machinename"] = Environment.MachineName;
data["osversion"] = Environment.OSVersion.VersionString;
data["os64bit"] = Environment.Is64BitOperatingSystem.ToString();
data["processorcount"] = Environment.ProcessorCount.ToString();
data["username"] = Environment.UserName;
data["runtimeversion"] = Environment.Version.ToString();
data["workingsetbytes"] = Environment.WorkingSet.ToString();
data["runningfor"] = (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString("d\\ hh\\:mm\\:ss");
data["threadcount"] = Process.GetCurrentProcess().Threads.Count.ToString();
data["fullexception"] = ex.ToString();
return data;
}
}
}