Skip to content

Commit 380ffdf

Browse files
committed
Small exception handling tweaks
- Added in code so the unhandled exception method can be tested while debugging - Try to define invariant culture and use it at critical points so crash logs come back in English. Will make providing support easier. - Unescape newline characters in serialization output
1 parent 90e761a commit 380ffdf

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

WinNUT_V2/WinNUT-Client/ApplicationEvents.vb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
'
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

10+
Imports System.Globalization
1011
Imports System.IO
12+
Imports System.Text.RegularExpressions
1113
Imports Microsoft.VisualBasic.ApplicationServices
1214
Imports Newtonsoft.Json
1315
Imports WinNUT_Client_Common
@@ -20,23 +22,31 @@ Namespace My
2022
' StartupNextInstance : Déclenché lors du lancement d'une application à instance unique et si cette application est déjà active.
2123
' NetworkAvailabilityChanged : Déclenché quand la connexion réseau est connectée ou déconnectée.
2224
Partial Friend Class MyApplication
25+
' Default culture for output so logs can be shared with the project.
26+
Private Shared DEF_CULTURE_INFO As CultureInfo = CultureInfo.InvariantCulture
2327

2428

2529
Private CrashBug_Form As New Form
2630
Private BtnClose As New Button
2731
Private BtnGenerate As New Button
2832
Private Msg_Crash As New Label
2933
Private Msg_Error As New TextBox
30-
Private exception As Exception
34+
35+
Private crashReportData As String
3136

3237
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
38+
' Uncomment below and comment out Handles line for _UnhandledException sub when debugging unhandled exceptions.
39+
' AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomainUnhandledException
3340
Init_Globals()
3441
LogFile.LogTracing("MyApplication_Startup complete.", LogLvl.LOG_DEBUG, Me)
3542
End Sub
3643

37-
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
44+
Private Sub AppDomainUnhandledException(sender As Object, e As System.UnhandledExceptionEventArgs)
45+
MyApplication_UnhandledException(sender, New UnhandledExceptionEventArgs(False, e.ExceptionObject))
46+
End Sub
47+
48+
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
3849
e.ExitApplication = False
39-
exception = e.Exception
4050

4151
With Msg_Crash
4252
.Location = New Point(6, 6)
@@ -87,6 +97,8 @@ Namespace My
8797
.Controls.Add(BtnGenerate)
8898
End With
8999

100+
crashReportData = GenerateCrashReport(e.Exception)
101+
90102
AddHandler BtnClose.Click, AddressOf Application.Close_Button_Click
91103
AddHandler BtnGenerate.Click, AddressOf Application.Generate_Button_Click
92104

@@ -96,9 +108,13 @@ Namespace My
96108
End Sub
97109

98110
Private Shared Function GenerateCrashReport(ex As Exception) As String
99-
Dim reportStream As New StringWriter()
111+
Dim jsonSerializerSettings As New JsonSerializerSettings()
112+
jsonSerializerSettings.Culture = DEF_CULTURE_INFO
113+
jsonSerializerSettings.Formatting = Formatting.Indented
114+
115+
Dim reportStream As New StringWriter(DEF_CULTURE_INFO)
100116
reportStream.WriteLine("WinNUT Bug Report")
101-
reportStream.WriteLine("Generated at " + DateTime.UtcNow.ToString("F"))
117+
reportStream.WriteLine("Generated at " + Date.UtcNow.ToString("F", DEF_CULTURE_INFO))
102118
reportStream.WriteLine()
103119
reportStream.WriteLine("OS Version: " & Computer.Info.OSVersion)
104120
reportStream.WriteLine("WinNUT Version: " & ProgramVersion)
@@ -124,7 +140,7 @@ Namespace My
124140
confCopy.Add(kvp.Key, newVal)
125141
Next
126142

127-
reportStream.WriteLine(JsonConvert.SerializeObject(confCopy, Formatting.Indented))
143+
reportStream.WriteLine(JsonConvert.SerializeObject(confCopy, jsonSerializerSettings))
128144
reportStream.WriteLine()
129145
Else
130146
reportStream.WriteLine("[EMPTY]")
@@ -134,28 +150,27 @@ Namespace My
134150
#Region "Exceptions"
135151
reportStream.WriteLine("==== Exception ====")
136152
reportStream.WriteLine()
137-
reportStream.WriteLine(JsonConvert.SerializeObject(ex, Formatting.Indented))
153+
reportStream.WriteLine(Regex.Unescape(JsonConvert.SerializeObject(ex, jsonSerializerSettings)))
138154
reportStream.WriteLine()
139155
#End Region
140156

141157
reportStream.WriteLine("==== Last Events ====")
142158

143159
LogFile.LastEvents.Reverse()
144160
reportStream.WriteLine()
145-
reportStream.WriteLine(JsonConvert.SerializeObject(LogFile.LastEvents, Formatting.Indented))
161+
reportStream.WriteLine(Regex.Unescape(JsonConvert.SerializeObject(LogFile.LastEvents, jsonSerializerSettings)))
146162

147163
Return reportStream.ToString()
148164
End Function
149165

150166
Private Sub Generate_Button_Click(sender As Object, e As EventArgs)
151-
Dim logFileName = "CrashReport_" + DateTime.Now.ToString("s").Replace(":", ".") + ".txt"
152-
Dim report = GenerateCrashReport(exception)
167+
Dim logFileName = "CrashReport_" + Date.Now.ToString("s").Replace(":", ".") + ".txt"
153168

154-
Computer.Clipboard.SetText(report)
169+
Computer.Clipboard.SetText(crashReportData)
155170

156171
Directory.CreateDirectory(TEMP_DATA_PATH)
157172
Dim CrashLog_Report = New StreamWriter(Path.Combine(TEMP_DATA_PATH, logFileName))
158-
CrashLog_Report.WriteLine(report)
173+
CrashLog_Report.WriteLine(crashReportData)
159174
CrashLog_Report.Close()
160175

161176
' Open an Explorer window to the crash log.

0 commit comments

Comments
 (0)