Skip to content

Commit 44efc90

Browse files
committed
boost(ErrorReporting): Add proper error reporting
using Sentry. Fixes #546
1 parent 155345c commit 44efc90

File tree

4 files changed

+35
-40
lines changed

4 files changed

+35
-40
lines changed

SoundSwitch/Framework/Logger/Configuration/LoggerConfigurator.cs

+10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.Diagnostics;
33
using System.IO;
44
using Serilog;
5+
using Serilog.Events;
56
using Serilog.Exceptions;
67
using SoundSwitch.Framework.Logger.Enricher;
8+
using SoundSwitch.Util;
79

810
namespace SoundSwitch.Framework.Logger.Configuration
911
{
@@ -24,6 +26,14 @@ public static void ConfigureLogger()
2426
rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3,
2527
flushToDiskInterval: TimeSpan.FromMinutes(10),
2628
outputTemplate: "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message} (at {Caller}){NewLine}{Exception}")
29+
.WriteTo.Sentry(o =>
30+
{
31+
o.InitializeSdk = false;
32+
o.Dsn = "https://[email protected]/5755327";
33+
o.MinimumBreadcrumbLevel = LogEventLevel.Debug;
34+
o.MinimumEventLevel = LogEventLevel.Error;
35+
o.Environment = AssemblyUtils.GetReleaseState().ToString();
36+
})
2737
.CreateLogger();
2838
var listener = new global::SerilogTraceListener.SerilogTraceListener();
2939
Trace.Listeners.Add(listener);

SoundSwitch/Program.cs

+18-39
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515

1616
using System;
1717
using System.Diagnostics;
18-
using System.IO;
19-
using System.IO.Compression;
2018
using System.Runtime.ExceptionServices;
2119
using System.Runtime.InteropServices;
2220
using System.Threading;
2321
using System.Windows.Forms;
22+
using Sentry;
2423
using Serilog;
2524
using SoundSwitch.Framework;
25+
using SoundSwitch.Framework.Configuration;
2626
using SoundSwitch.Framework.Logger.Configuration;
27-
using SoundSwitch.Framework.Minidump;
2827
using SoundSwitch.Framework.NotificationManager;
2928
using SoundSwitch.Framework.WinApi;
3029
using SoundSwitch.Localization.Factory;
3130
using SoundSwitch.Model;
3231
using SoundSwitch.Util;
32+
using SoundSwitch.Util.Url;
3333

3434
namespace SoundSwitch
3535
{
@@ -42,13 +42,15 @@ internal static class Program
4242
[STAThread]
4343
private static void Main()
4444
{
45+
using var _ = SentrySdk.Init(options =>
46+
{
47+
options.Dsn = "https://[email protected]/5755327";
48+
options.Environment = AssemblyUtils.GetReleaseState().ToString();
49+
});
4550
InitializeLogger();
4651
Log.Information("Application Starts");
4752
#if !DEBUG
48-
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
49-
{
50-
HandleException((Exception) args.ExceptionObject);
51-
};
53+
AppDomain.CurrentDomain.UnhandledException += (sender, args) => { HandleException((Exception) args.ExceptionObject); };
5254

5355
Log.Information("Set Exception Handler");
5456
Application.ThreadException += Application_ThreadException;
@@ -134,7 +136,6 @@ private static void Main()
134136
WindowsAPIAdapter.Stop();
135137
MMNotificationClient.Instance.Dispose();
136138
Log.CloseAndFlush();
137-
138139
}
139140

140141
/// <summary>
@@ -147,6 +148,8 @@ private static void InitializeLogger()
147148
$"{Application.ProductName} {AssemblyUtils.GetReleaseState()} ({Application.ProductVersion})");
148149
Log.Information($"OS: {Environment.OSVersion}");
149150
Log.Information($"Framework: {Environment.Version}");
151+
152+
SentrySdk.ConfigureScope(scope => { scope.AddAttachment(AppConfigs.Configuration.FileLocation); });
150153
}
151154

152155
/// <summary>
@@ -174,8 +177,8 @@ private static void HandleException(Exception exception)
174177
{
175178
if (exception == null)
176179
return;
177-
var zipFile = Path.Combine(ApplicationPath.AppData,
178-
$"{Application.ProductName}-crashlog-{DateTime.UtcNow.Date.Day}_{DateTime.UtcNow.Date.Month}_{DateTime.UtcNow.Date.Year}.zip");
180+
var eventId = SentrySdk.CaptureException(exception);
181+
179182
var exceptionMessage = exception.Message;
180183
if (exception.InnerException != null)
181184
{
@@ -187,39 +190,15 @@ private static void HandleException(Exception exception)
187190
188191
{exceptionMessage}
189192
190-
Do you want to save a log of the error that occurred?
191-
This could be useful to fix bugs. Please post this file in the issues section
192-
File Location: {zipFile}";
193+
Would you like to share more information with the developers?";
193194
var result = MessageBox.Show(message, $@"{Application.ProductName} crashed...", MessageBoxButtons.YesNo,
194195
MessageBoxIcon.Error);
195196

196-
if (result == DialogResult.Yes)
197-
{
198-
using (new HourGlass())
199-
{
200-
var fileName = Path.Combine(ApplicationPath.Default, Environment.MachineName + ".dmp");
201-
using (
202-
var fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite,
203-
FileShare.Write))
204-
{
205-
MiniDump.Write(fs.SafeFileHandle,
206-
MiniDump.Option.Normal | MiniDump.Option.WithThreadInfo | MiniDump.Option.WithHandleData |
207-
MiniDump.Option.WithDataSegs, MiniDump.ExceptionInfo.Present);
208-
}
209-
210-
Log.Fatal(exception, "Exception Occurred ");
211-
212-
if (File.Exists(zipFile))
213-
{
214-
File.Delete(zipFile);
215-
}
216-
217-
Log.CloseAndFlush();
218-
219-
ZipFile.CreateFromDirectory(ApplicationPath.Default, zipFile);
220-
}
197+
if (result != DialogResult.Yes) return;
221198

222-
Process.Start("explorer.exe", "/select," + @zipFile);
199+
using (new HourGlass())
200+
{
201+
BrowserUtil.OpenUrl($"https://soundswitch.aaflalo.me/#sentry?eventId={eventId}");
223202
}
224203
}
225204
}

SoundSwitch/SoundSwitch.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<PackageReference Include="NAudio" Version="2.0.0" />
4040
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
4141
<PackageReference Include="RailSharp" Version="1.0.0" />
42+
<PackageReference Include="Sentry.Serilog" Version="3.3.4" />
4243
<PackageReference Include="Serilog" Version="2.10.0" />
4344
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
4445
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />

SoundSwitch/Util/AssemblyUtils.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static class AssemblyUtils
2222
public enum ReleaseState
2323
{
2424
Stable,
25-
Beta
25+
Beta,
26+
Debug
2627
}
2728

2829
/// <summary>
@@ -43,7 +44,11 @@ public static AssemblyConfigurationAttribute GetAssemblyConfigurationAttribute()
4344
/// <returns></returns>
4445
public static ReleaseState GetReleaseState()
4546
{
47+
#if DEBUG
48+
return ReleaseState.Debug;
49+
#else
4650
return GetAssemblyConfigurationAttribute().Configuration == "Beta" ? ReleaseState.Beta : ReleaseState.Stable;
51+
#endif
4752
}
4853
}
4954
}

0 commit comments

Comments
 (0)