Skip to content

Commit 1cdc152

Browse files
Increment version to 1.8.1 and enhance bug reporting
- Updated project file to version 1.8.1. - Refactored bug reporting to include detailed environment and exception information. - Simplified bug report generation and improved modularity. - Added `isSevenZipAvailable` check to BugReportService. - Removed redundant logging and streamlined exception handling.
1 parent 69d149e commit 1cdc152

File tree

5 files changed

+95
-128
lines changed

5 files changed

+95
-128
lines changed

BatchConvertToCHD/AboutWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e
3535
// Notify developer
3636
if (App.SharedBugReportService != null)
3737
{
38-
_ = App.SharedBugReportService.SendBugReportAsync($"Error opening URL: {e.Uri.AbsoluteUri}. Exception: {ex.Message}");
38+
_ = App.SharedBugReportService.SendBugReportAsync($"Error opening URL: {e.Uri.AbsoluteUri}", ex);
3939
}
4040

4141
// Notify user

BatchConvertToCHD/App.xaml.cs

Lines changed: 7 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System.Globalization;
21
using System.IO;
3-
using System.Text;
42
using System.Windows;
53
using System.Windows.Threading;
64
using BatchConvertToCHD.Services;
75
using SevenZip;
8-
using Microsoft.Win32;
96

107
namespace BatchConvertToCHD;
118

@@ -25,21 +22,18 @@ public partial class App : IDisposable
2522

2623
public App()
2724
{
25+
// Initialize SevenZipSharp library path first to determine its availability
26+
InitializeSevenZipSharp();
27+
2828
// Initialize the bug report service as a shared instance
29-
SharedBugReportService = new BugReportService(AppConfig.BugReportApiUrl, AppConfig.BugReportApiKey, AppConfig.ApplicationName);
29+
SharedBugReportService = new BugReportService(AppConfig.BugReportApiUrl, AppConfig.BugReportApiKey, AppConfig.ApplicationName, IsSevenZipAvailable);
3030
_bugReportService = SharedBugReportService;
3131

3232
// Set up global exception handling
3333
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
3434
DispatcherUnhandledException += App_DispatcherUnhandledException;
3535
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
3636

37-
// Initialize SevenZipSharp library path
38-
InitializeSevenZipSharp();
39-
40-
// Log environment details for debugging
41-
LogEnvironmentDetails();
42-
4337
// Register the Exit event handler
4438
Exit += App_Exit;
4539
}
@@ -80,12 +74,10 @@ private async void ReportException(Exception exception, string source)
8074
{
8175
try
8276
{
83-
var message = BuildExceptionReport(exception, source);
84-
8577
// Notify the developer using the shared service instance
8678
if (_bugReportService != null)
8779
{
88-
await _bugReportService.SendBugReportAsync(message);
80+
await _bugReportService.SendBugReportAsync($"Unhandled Exception from {source}", exception);
8981
}
9082
}
9183
catch
@@ -94,47 +86,6 @@ private async void ReportException(Exception exception, string source)
9486
}
9587
}
9688

97-
internal static string BuildExceptionReport(Exception exception, string source)
98-
{
99-
var sb = new StringBuilder();
100-
sb.AppendLine(CultureInfo.InvariantCulture, $"Error Source: {source}");
101-
sb.AppendLine(CultureInfo.InvariantCulture, $"Date and Time: {DateTime.Now}");
102-
sb.AppendLine(CultureInfo.InvariantCulture, $"OS Version: {Environment.OSVersion}");
103-
sb.AppendLine(CultureInfo.InvariantCulture, $".NET Version: {Environment.Version}");
104-
sb.AppendLine();
105-
106-
// Add exception details
107-
sb.AppendLine("Exception Details:");
108-
AppendExceptionDetails(sb, exception);
109-
110-
return sb.ToString();
111-
}
112-
113-
internal static void AppendExceptionDetails(StringBuilder sb, Exception exception, int level = 0)
114-
{
115-
while (true)
116-
{
117-
var indent = new string(' ', level * 2);
118-
119-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Type: {exception.GetType().FullName}");
120-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Message: {exception.Message}");
121-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Source: {exception.Source}");
122-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}StackTrace:");
123-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}{exception.StackTrace}");
124-
125-
// If there's an inner exception, include it too
126-
if (exception.InnerException != null)
127-
{
128-
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Inner Exception:");
129-
exception = exception.InnerException;
130-
level += 1;
131-
continue;
132-
}
133-
134-
break;
135-
}
136-
}
137-
13889
private void InitializeSevenZipSharp()
13990
{
14091
try
@@ -153,7 +104,7 @@ private void InitializeSevenZipSharp()
153104
var errorMessage = $"Could not find the required 7-Zip library: {dllName} in {AppDomain.CurrentDomain.BaseDirectory}";
154105
if (_bugReportService != null)
155106
{
156-
_ = _bugReportService.SendBugReportAsync(errorMessage);
107+
_ = _bugReportService.SendBugReportAsync(errorMessage, null);
157108
}
158109

159110
IsSevenZipAvailable = false;
@@ -164,72 +115,13 @@ private void InitializeSevenZipSharp()
164115
// Notify developer
165116
if (_bugReportService != null)
166117
{
167-
_ = _bugReportService.SendBugReportAsync(ex.Message);
118+
_ = _bugReportService.SendBugReportAsync("Error initializing 7-Zip library", ex);
168119
}
169120

170121
IsSevenZipAvailable = false;
171122
}
172123
}
173124

174-
/// <summary>
175-
/// Logs detailed environment information for debugging
176-
/// </summary>
177-
private void LogEnvironmentDetails()
178-
{
179-
try
180-
{
181-
var sb = new StringBuilder();
182-
sb.AppendLine("=== Application Environment ===");
183-
sb.AppendLine(CultureInfo.InvariantCulture, $"OS Version: {Environment.OSVersion}");
184-
sb.AppendLine(CultureInfo.InvariantCulture, $".NET Version: {Environment.Version}");
185-
sb.AppendLine(CultureInfo.InvariantCulture, $"Process Architecture: {Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")}");
186-
sb.AppendLine(CultureInfo.InvariantCulture, $"Is 64-bit Process: {Environment.Is64BitProcess}");
187-
sb.AppendLine(CultureInfo.InvariantCulture, $"Base Directory: {AppDomain.CurrentDomain.BaseDirectory}");
188-
sb.AppendLine(CultureInfo.InvariantCulture, $"Temp Path: {Path.GetTempPath()}");
189-
sb.AppendLine(CultureInfo.InvariantCulture, $"User: {Environment.UserName}");
190-
191-
// Check for common security software
192-
var securitySoftware = new List<string>();
193-
try
194-
{
195-
using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
196-
if (key != null)
197-
{
198-
foreach (var subKeyName in key.GetSubKeyNames())
199-
{
200-
using var subKey = key.OpenSubKey(subKeyName);
201-
var displayName = subKey?.GetValue("DisplayName")?.ToString() ?? "";
202-
if (displayName.Contains("Defender") || displayName.Contains("Antivirus") ||
203-
displayName.Contains("Security") || displayName.Contains("ESET") ||
204-
displayName.Contains("Norton") || displayName.Contains("McAfee"))
205-
{
206-
securitySoftware.Add(displayName);
207-
}
208-
}
209-
}
210-
}
211-
catch
212-
{
213-
/* ignore */
214-
}
215-
216-
if (securitySoftware.Count != 0)
217-
{
218-
sb.AppendLine(CultureInfo.InvariantCulture, $"Detected Security Software: {string.Join("; ", securitySoftware)}");
219-
}
220-
221-
// Log to the bug report service if available
222-
if (_bugReportService != null)
223-
{
224-
_ = _bugReportService.SendBugReportAsync(sb.ToString());
225-
}
226-
}
227-
catch (Exception)
228-
{
229-
// Silently ignore logging errors
230-
}
231-
}
232-
233125
/// <inheritdoc />
234126
/// <summary>
235127
/// Releases all resources used by the App.

BatchConvertToCHD/BatchConvertToCHD.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<UseWPF>true</UseWPF>
99
<ApplicationIcon>icon\icon.ico</ApplicationIcon>
1010
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
11-
<AssemblyVersion>1.8</AssemblyVersion>
12-
<FileVersion>1.8</FileVersion>
11+
<AssemblyVersion>1.8.1</AssemblyVersion>
12+
<FileVersion>1.8.1</FileVersion>
1313
<IsPackable>false</IsPackable>
1414
<NeutralLanguage>en</NeutralLanguage>
1515
<DebugType>embedded</DebugType>

BatchConvertToCHD/MainWindow.xaml.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.Globalization;
44
using System.IO;
5-
using System.Text;
65
using System.Text.RegularExpressions;
76
using System.Windows;
87
using System.Windows.Controls;
@@ -144,7 +143,7 @@ private void LogEnvironmentDetails()
144143
{
145144
try
146145
{
147-
var sb = new StringBuilder();
146+
var sb = new System.Text.StringBuilder();
148147
sb.AppendLine("=== Environment Details ===");
149148
sb.AppendLine(CultureInfo.InvariantCulture, $"OS: {Environment.OSVersion}");
150149
sb.AppendLine(CultureInfo.InvariantCulture, $"User: {Environment.UserName}");
@@ -870,10 +869,7 @@ private async Task ReportBugAsync(string msg, Exception? ex = null)
870869
{
871870
try
872871
{
873-
var sb = new StringBuilder();
874-
sb.AppendLine(CultureInfo.InvariantCulture, $"Error: {msg}");
875-
if (ex != null) App.AppendExceptionDetails(sb, ex);
876-
if (App.SharedBugReportService != null) await App.SharedBugReportService.SendBugReportAsync(sb.ToString());
872+
if (App.SharedBugReportService != null) await App.SharedBugReportService.SendBugReportAsync(msg, ex);
877873
}
878874
catch
879875
{

BatchConvertToCHD/Services/BugReportService.cs

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1+
using System.Globalization;
2+
using System.IO;
13
using System.Net.Http;
24
using System.Net.Http.Json;
5+
using System.Reflection;
6+
using System.Text;
37

48
namespace BatchConvertToCHD.Services;
59

610
/// <summary>
711
/// Service responsible for sending bug reports to the BugReport API
812
/// </summary>
9-
public class BugReportService(string apiUrl, string apiKey, string applicationName) : IDisposable
13+
public class BugReportService(string apiUrl, string apiKey, string applicationName, bool isSevenZipAvailable) : IDisposable
1014
{
1115
private readonly HttpClient _httpClient = new();
1216
private readonly string _apiUrl = apiUrl;
1317
private readonly string _apiKey = apiKey;
1418
private readonly string _applicationName = applicationName;
19+
private readonly bool _isSevenZipAvailable = isSevenZipAvailable;
1520

1621
/// <summary>
1722
/// Sends a bug report to the API
1823
/// </summary>
19-
/// <param name="message">The error message or bug report</param>
24+
/// <param name="message">A summary of the error or bug report</param>
25+
/// <param name="ex">The exception object, if available</param>
2026
/// <returns>A task representing the asynchronous operation</returns>
21-
public async Task<bool> SendBugReportAsync(string message)
27+
public async Task<bool> SendBugReportAsync(string message, Exception? ex = null)
2228
{
29+
// Build the full report message
30+
var fullReport = BuildExceptionReport(message, ex);
31+
2332
try
2433
{
2534
_httpClient.DefaultRequestHeaders.Clear();
@@ -28,7 +37,7 @@ public async Task<bool> SendBugReportAsync(string message)
2837
// Create the request payload
2938
var content = JsonContent.Create(new
3039
{
31-
message,
40+
message = fullReport,
3241
applicationName = _applicationName
3342
});
3443

@@ -45,6 +54,76 @@ public async Task<bool> SendBugReportAsync(string message)
4554
}
4655
}
4756

57+
private string BuildExceptionReport(string message, Exception? exception)
58+
{
59+
var sb = new StringBuilder();
60+
sb.AppendLine(GetEnvironmentDetailsReport());
61+
sb.AppendLine();
62+
sb.AppendLine("=== Error Details ===");
63+
sb.AppendLine(CultureInfo.InvariantCulture, $"Custom Message: {message}");
64+
sb.AppendLine(CultureInfo.InvariantCulture, $"Date and Time: {DateTime.Now}");
65+
66+
// Add exception details if provided
67+
if (exception != null)
68+
{
69+
sb.AppendLine("Exception Details:");
70+
AppendExceptionDetails(sb, exception);
71+
}
72+
73+
return sb.ToString();
74+
}
75+
76+
private static void AppendExceptionDetails(StringBuilder sb, Exception exception, int level = 0)
77+
{
78+
while (true)
79+
{
80+
var indent = new string(' ', level * 2);
81+
82+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Type: {exception.GetType().FullName}");
83+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Message: {exception.Message}");
84+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Source: {exception.Source}");
85+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}StackTrace:");
86+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}{exception.StackTrace}");
87+
88+
// If there's an inner exception, include it too
89+
if (exception.InnerException != null)
90+
{
91+
sb.AppendLine(CultureInfo.InvariantCulture, $"{indent}Inner Exception:");
92+
exception = exception.InnerException;
93+
level += 1;
94+
continue;
95+
}
96+
97+
break;
98+
}
99+
}
100+
101+
private string GetEnvironmentDetailsReport()
102+
{
103+
try
104+
{
105+
var sb = new StringBuilder();
106+
sb.AppendLine("=== Environment Details ===");
107+
sb.AppendLine(CultureInfo.InvariantCulture, $"OS Version: {Environment.OSVersion}");
108+
sb.AppendLine(CultureInfo.InvariantCulture, $"Windows Version: {Environment.OSVersion.Version}");
109+
sb.AppendLine(CultureInfo.InvariantCulture, $".NET Version: {Environment.Version}");
110+
sb.AppendLine(CultureInfo.InvariantCulture, $"Process Architecture: {(Environment.Is64BitProcess ? "x64" : "x86")}");
111+
sb.AppendLine(CultureInfo.InvariantCulture, $"Processor Count: {Environment.ProcessorCount}");
112+
sb.AppendLine(CultureInfo.InvariantCulture, $"Application Version: {Assembly.GetExecutingAssembly().GetName().Version}");
113+
sb.AppendLine(CultureInfo.InvariantCulture, $"Base Directory: {AppDomain.CurrentDomain.BaseDirectory}");
114+
sb.AppendLine(CultureInfo.InvariantCulture, $"Temp Path: {Path.GetTempPath()}");
115+
sb.AppendLine(CultureInfo.InvariantCulture, $"User: {Environment.UserName}");
116+
sb.AppendLine(CultureInfo.InvariantCulture, $"CHDMAN Available: {File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "chdman.exe"))}");
117+
sb.AppendLine(CultureInfo.InvariantCulture, $"MAXCSO Available: {File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "maxcso.exe"))}");
118+
sb.AppendLine(CultureInfo.InvariantCulture, $"7-Zip Available: {_isSevenZipAvailable}");
119+
return sb.ToString();
120+
}
121+
catch (Exception ex)
122+
{
123+
return $"Failed to get environment details: {ex.Message}";
124+
}
125+
}
126+
48127
public void Dispose()
49128
{
50129
_httpClient?.Dispose();

0 commit comments

Comments
 (0)