-
Notifications
You must be signed in to change notification settings - Fork 3
/
Crash.cs
133 lines (115 loc) · 5.31 KB
/
Crash.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ProSnap
{
internal static class Crash
{
internal static void SubmitCrashReport()
{
StringBuilder diag = new StringBuilder();
diag.AppendLine("Application:\n" + Application.ProductName);
diag.AppendLine("\nVersion:\n" + Application.ProductVersion);
diag.AppendLine("\nDate:\n" + DateTime.Now.ToUniversalTime().ToString() + " UTC");
diag.AppendLine("\nOperating System:\n" + Environment.OSVersion.VersionString);
diag.AppendLine("\nPlatform:\n" + (IntPtr.Size == 8 ? "64" : "32") + " bit");
diag.AppendLine("\nRunning As:\n" + (isElevated ? "Administrator" : "Standard User"));
diag.AppendLine("\nFramework Version:\n" + FrameworkVersion);
diag.AppendLine("\nUAC Enabled:\n" + UACEnabled);
ReportListener reporter = Trace.Listeners.Cast<TraceListener>().Where(tl => tl is ReportListener).FirstOrDefault() as ReportListener;
diag.AppendLine("\nDebug Log:\n" + string.Join("\n", string.Join("\n", reporter.Messages.Select(m => string.Format("<{0}> {1}: {2}", m.Timestamp, m.Category, m.Message)).ToArray())));
UploadReport("http://factormystic.net/prosnap/feedback/report.php", Crash.Gzip(diag.ToString()));
}
private static byte[] Gzip(string data)
{
Trace.WriteLine("Staring gzip...", string.Format("Crash.Gzip [{0}]", System.Threading.Thread.CurrentThread.Name));
using (var ms = new MemoryStream())
{
var gzip = new GZipStream(ms, CompressionMode.Compress);
gzip.Write(UTF8Encoding.UTF8.GetBytes(data), 0, UTF8Encoding.UTF8.GetBytes(data).Length);
gzip.Close();
Trace.WriteLine("Done.", string.Format("Crash.Gzip [{0}]", System.Threading.Thread.CurrentThread.Name));
return ms.ToArray();
}
}
private static void UploadReport(string url, byte[] data)
{
Trace.WriteLine("Starting UploadReport...", string.Format("Crash.UploadReport [{0}]", System.Threading.Thread.CurrentThread.Name));
string result = string.Empty;
try
{
string report = Path.Combine(Application.LocalUserAppDataPath, "report.txt.gz");
File.WriteAllBytes(report, data);
using (var wc = new WebClient())
{
wc.Headers.Add("Content-Type", "binary/octet-stream");
result = UTF8Encoding.UTF8.GetString(wc.UploadFile(new Uri(url), report));
}
}
catch (WebException e)
{
Trace.WriteLine(string.Format("UploadReport WebException: '{0}'", e.GetBaseException()), string.Format("Crash.UploadReport [{0}]", System.Threading.Thread.CurrentThread.Name));
}
Trace.WriteLine(string.Format("UploadReport complete: '{0}'", result), string.Format("Crash.UploadReport [{0}]", System.Threading.Thread.CurrentThread.Name));
}
private static bool UACEnabled
{
get
{
int enabled = 0;
try
{
object key_value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "EnableLUA", 0);
if (key_value is int)
enabled = (int)key_value;
else if (!int.TryParse(key_value.ToString(), out enabled))
{
Trace.WriteLine("*UACEnabled: TryParse failed on " + key_value);
}
}
catch (Exception e)
{
Trace.WriteLine("*UACEnabled Exception: " + e.ToString());
}
return enabled == 1;
}
}
private static Version FrameworkVersion
{
get
{
//based on http://msdn.microsoft.com/en-us/kb/kb00318785.aspx
if (_framework != null)
return _framework;
try
{
string[] dirs = Directory.GetDirectories(Environment.ExpandEnvironmentVariables(@"%systemroot%\Microsoft.NET\Framework"), "v?.*", SearchOption.TopDirectoryOnly);
string latest = dirs[dirs.Length - 1];
_framework = new Version(latest.Remove(0, Path.GetDirectoryName(latest).Length + 1).Trim(new char[] { 'v' }));
return _framework;
}
catch (Exception e)
{
Trace.WriteLine("*FrameworkVersion: File based version detection failed; " + e.GetBaseException().Message);
return new Version();
}
}
}
private static Version _framework;
private static bool isElevated
{
get
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
}