I have have a Solution that occasionally crashes and I would like to log the exception. I have added code to handle unhandled exception, but it does not get called. Perhaps this is related to inheritance.
To simplify the investigation, I created an experimental Solution to test the exception handling. It has two Projects, WindowsAppSource and WindowsAppTest. WindowsAppTest inherits WindowsAppSource. A divide by zero is used to create an exception.
WindowsAppSource has one control, Button1:
WindowsAppTest inherits WindowsAppSource and overrides Button1:Code:Protected WithEvents Button1 As Button - - - Protected Overridable Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub
Both projects have the same methods added to handle unhandled exceptions, as suggested on this fine forum, but none of these get called.Code:Public Class Form1 Inherits WindowsAppSource.Form1 - - - Protected Overrides Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim z As Integer = 0 Dim i As Integer = 1 \ z End Sub End Class
In ApplicationEvents.vb (of both projects):
In Application.Designer.vb (of both projects):Code:Partial Friend Class MyApplication Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException Dim exLogPath As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "UnhandledExceptions") If Not System.IO.Directory.Exists(exLogPath) Then MkDir(exLogPath? Dim fullPath As String = System.IO.Path.Combine(exLogPath, $"ExceptionLog-{Format(Now, "yyyy.MM.dd")}.txt") Using sw = New IO.StreamWriter(fullPath, True) sw.WriteLine($"{vbCrLf}{Format(Now, "yyyy/MM/dd HH:mm:ss")}{vbCrLf}Sender: {sender}{vbCrLf}{e.Exception}{vbCrLf}") End Using End Sub End Class
(I kept getting "403 Forbidden" with the closing parentheses in the MkDir lines, so I replaced them with question marks)Code:Namespace My Partial Friend Class MyApplication <Global.System.Diagnostics.DebuggerStepThroughAttribute()> Public Sub New() ... <Global.System.Diagnostics.DebuggerStepThroughAttribute()> Protected Overrides Sub OnCreateMainForm() ... Private Sub MyApplicationUnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException Dim exLogPath As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "UnhandledExceptions") If Not System.IO.Directory.Exists(exLogPath) Then MkDir(exLogPath? Dim fullPath As String = System.IO.Path.Combine(exLogPath, $"ExceptionLog-{Format(Now, "yyyy.MM.dd")}.txt") Using sw = New IO.StreamWriter(fullPath, True) sw.WriteLine($"{vbCrLf}{Format(Now, "yyyy/MM/dd HH:mm:ss")}{vbCrLf}Sender: {sender}{vbCrLf}{e.Exception}{vbCrLf}") End Using End Sub End Class End Namespace
I will appreciate any suggestions of how the exception can be logged.




Reply With Quote
