Namespace Base.ExceptionHandling
''' <summary>
''' Behandelt Ausnahmefehler, das Event OnError wird ausgelöst wenn bei der Fehlerbehandlung etwas schief läuft
''' </summary>
''' <remarks>Fehlerbehandlungsklasse Revision 0</remarks>
Public Class ExtendedExceptionHandling
Public Enum RotationType
None = 0
Daily = 1
Weekly = 2
Monthly = 3
Yearly = 4
End Enum
Private _ex As Exception
Private _Logfile As String = ""
Private _LogToFile As Boolean
Private _RaisePopup As Boolean
Private _Disabled As Boolean = False
Private _LogfileRotation As RotationType
Private _InternalLogfilename As String = ""
Private _WriteToWindowsEventLog As Boolean = False
Private _EventlogAppName As String = "ApplicationName"
Private _EventlogLogName As String = "ApplicationGroupName"
Public Event OnError(ByVal Message As String)
''' <summary>
''' Name der Anwendung der in der Windows-Ereignisanzeige angezeigt wird
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property EventlogApplicationName() As String
Get
Return _EventlogAppName
End Get
Set(ByVal value As String)
_EventlogAppName = value
End Set
End Property
''' <summary>
''' Name der Ereignisanzeigen-Gruppe (z.B. "Anwendung", "Sicherheit" usw)
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property EventlogLogfileGroupName() As String
Get
Return _EventlogLogName
End Get
Set(ByVal value As String)
_EventlogLogName = value
End Set
End Property
''' <summary>
''' Aktiviert/Deaktiviert das Schreiben in die Windows-Ereignisanzeige
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>Mit EventlogApplicationName und EventlogLogfileGroupName kann das Ziel angegeben werden</remarks>
Public Property WriteToWindowsEventLog() As Boolean
Get
Return _WriteToWindowsEventLog
End Get
Set(ByVal value As Boolean)
_WriteToWindowsEventLog = value
End Set
End Property
''' <summary>
''' Absoluter Pfad zur Logdatei
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property LogfilePath() As String
Get
Return _Logfile
End Get
Set(ByVal value As String)
_Logfile = value
SetNewLogfileName()
End Set
End Property
''' <summary>
''' Schaltet das Logging in eine Datei
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property LogToFile() As Boolean
Get
Return _LogToFile
End Get
Set(ByVal value As Boolean)
If _Logfile <> "" Or value = False Then
_LogToFile = value
Else
RaiseEvent OnError("Die Logdatei muss angegeben sein bevor der das Logging in eine Datei angeschaltet wird.")
End If
End Set
End Property
''' <summary>
''' Schaltet ob IMMER ein Fehlerfenster angezeigt werden soll
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property RaisePopupOnException() As Boolean
Get
Return RaisePopupOnException
End Get
Set(ByVal value As Boolean)
_RaisePopup = value
End Set
End Property
''' <summary>
''' Schaltet die Fehlerbehandlung über diese Klasse an/aus
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Disabled() As Boolean
Get
Return _Disabled
End Get
Set(ByVal value As Boolean)
_Disabled = value
End Set
End Property
''' <summary>
''' Schaltet die Fehlerbehandlung über diese Klasse an/aus
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Enabled() As Boolean
Get
Return Not (_Disabled)
End Get
Set(ByVal value As Boolean)
_Disabled = Not (value)
End Set
End Property
''' <summary>
''' Gibt an in welchem Intervall eine neue Logdatei erzeugt wird
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property LogfileRotation() As RotationType
Get
Return _LogfileRotation
End Get
Set(ByVal value As RotationType)
_LogfileRotation = value
SetNewLogfileName()
End Set
End Property
''' <summary>
''' Setzt den neuen Namen für tägliche/wöchentliche/monatliche Logfiles
''' </summary>
''' <remarks></remarks>
Private Sub SetNewLogfileName()
_InternalLogfilename = _Logfile
Dim FileNameParts() As String = _Logfile.Split(".")
If FileNameParts.Length < 2 Then
RaiseEvent OnError("Der Logfile-Dateiname muss mit Erweiterung (z.B. 'log.txt') angegeben werden, wenn tägliche Logfiles erzeugt werden sollen.")
Exit Sub
End If
Dim FileNameExtension As String = FileNameParts(FileNameParts.Length - 1)
Dim LogRot As String = ""
Select Case _LogfileRotation
Case RotationType.Daily : LogRot = " " & DateAndTime.Now.Year & "-" & DateAndTime.Now.Month & "-" & DateAndTime.Now.Day
Case RotationType.Weekly : LogRot = " " & DateAndTime.Now.Year & "-W" & DatePart(DateInterval.WeekOfYear, Date.Now)
Case RotationType.Monthly : LogRot = " " & DateAndTime.Now.Year & "-M" & DateAndTime.Now.Month
Case RotationType.Yearly : LogRot = " " & DateAndTime.Now.Year
End Select
_InternalLogfilename = Left(_Logfile, Len(_Logfile) - Len(FileNameExtension) - 1) & _
LogRot & "." & FileNameExtension
End Sub
''' <summary>
''' Konstruktor
''' </summary>
''' <remarks>Defaults: DebugMode = false, LogToFile = false, LogfileRotation = none, RaisePopup = true</remarks>
Public Sub New()
_Logfile = ""
_LogfileRotation = RotationType.None
_LogToFile = False
_RaisePopup = True
End Sub
''' <summary>
''' Konstruktor
''' </summary>
''' <param name="LogfilePath"></param>
''' <param name="LogfileRotation"></param>
''' <param name="RaisePopup"></param>
''' <remarks></remarks>
Public Sub New(ByVal LogfilePath As String, Optional ByVal LogfileRotation As RotationType = RotationType.Weekly, Optional ByVal RaisePopup As Boolean = True, Optional ByVal WriteToWindowsEventLog As Boolean = False)
_Logfile = LogfilePath
_LogToFile = True
_LogfileRotation = LogfileRotation
_RaisePopup = RaisePopup
_WriteToWindowsEventLog = WriteToWindowsEventLog
SetNewLogfileName()
End Sub
''' <summary>
''' Behandelt einen Ausnahmefehler
''' </summary>
''' <param name="ex"></param>
''' <remarks>Globale Einstellung für Dateilogging und Popupfenster werden berücksichtigt</remarks>
Public Sub HandleException(ByVal ex As Exception)
HandleException(ex, "", _RaisePopup)
End Sub
''' <summary>
''' Behandelt einen Ausnahmefehler
''' </summary>
''' <param name="ex"></param>
''' <param name="Remarks">Bemerkungen</param>
''' <remarks>Globale Einstellung für Dateilogging und Popupfenster werden berücksichtigt</remarks>
Public Sub HandleException(ByVal ex As Exception, ByVal Remarks As String)
HandleException(ex, Remarks, _RaisePopup)
End Sub
''' <summary>
''' Behandelt einen Ausnahmefehler
''' </summary>
''' <param name="ex"></param>
''' <param name="RaisePopup">Popupfenster anzeigen?</param>
''' <remarks>Globale Einstellung für Dateilogging wird berücksichtigt</remarks>
Public Sub HandleException(ByVal ex As Exception, ByVal RaisePopup As Boolean)
HandleException(ex, "", RaisePopup)
End Sub
''' <summary>
''' Behandelt einen Ausnahmefehler
''' </summary>
''' <param name="ex"></param>
''' <param name="Remarks">Bemerkungen</param>
''' <param name="RaisePopup">Popup Fenster anzeigen?</param>
''' <remarks>Globale Einstellung für Dateilogging wird berücksichtigt</remarks>
Public Sub HandleException(ByVal ex As Exception, ByVal Remarks As String, ByVal RaisePopup As Boolean)
'Fehlerbehandlung abgeschaltet?
If _Disabled = True Then
RaiseEvent OnError(ex.Message)
Throw ex
Exit Sub
End If
Dim InnerEx As Exception = Nothing
Dim LogMessage As String = ""
Dim WinMessage As String = ""
Dim Message As String = ex.Message
Dim ModuleName As String
If Not ex.TargetSite Is Nothing Then
ModuleName = ex.TargetSite.ToString
Else
ModuleName = "*unbekannt*"
End If
Dim Trace As String = ex.StackTrace
LogMessage = vbCrLf & DateAndTime.Now & " -----------------------------------------------------" & vbCrLf
LogMessage += "[EXCEPTION in Modul: " & ModuleName & "] "
If Remarks <> "" Then LogMessage += "[" & Remarks & "]"
LogMessage += vbCrLf & "[MESSAGE] " & ex.Message & vbCrLf
LogMessage += "[STACKTRACE] " & Trace
WinMessage = "Fehler in Modul " & ModuleName & ":" & vbCr
WinMessage += ex.Message
Try
InnerEx = ex.InnerException
'Inner Exception hinzufügen wenn möglich
If Not InnerEx Is Nothing Then
LogMessage += vbCrLf & "[INNER EXCEPTION in Modul " & InnerEx.TargetSite.ToString & "] " & vbCrLf
LogMessage += "[INNER STACKTRACE] " & InnerEx.StackTrace
WinMessage += vbCr & "Innere Ausnahme in Modul " & InnerEx.TargetSite.ToString & ":" & vbCr
WinMessage += InnerEx.Message
End If
'In Datei loggen
If _LogToFile = True Then
FileOpen(93, _InternalLogfilename, OpenMode.Append, OpenAccess.ReadWrite)
Print(93, LogMessage)
FileClose(93)
End If
'Popup Fenster anzeigen
If RaisePopup = True Then MsgBox(WinMessage, MsgBoxStyle.Critical, "Programmfehler")
'Windows Eventlog
If _WriteToWindowsEventLog = True Then WriteToEventLog(LogMessage, EventLogEntryType.Error)
Catch exp As Exception
RaiseEvent OnError("Fehler beim Behandeln eines Ausnahmefehlers: " & exp.Message & " - " & exp.StackTrace.ToString)
End Try
End Sub
''' <summary>
''' Schreibt ins Windows-Log
''' </summary>
''' <param name="Entry">Nachricht</param>
''' <param name="EventType">Typ</param>
''' <remarks></remarks>
Private Sub WriteToEventLog(ByVal Entry As String, ByVal EventType As EventLogEntryType)
Dim objEventLog As New EventLog
Try
'Applikation registrieren, wenn unbekannt
If Not EventLog.SourceExists(_EventlogAppName) Then
EventLog.CreateEventSource(_EventlogAppName, _EventlogLogName)
End If
objEventLog.Source = _EventlogAppName
'Schreibt in das Eventlog
objEventLog.WriteEntry(Entry, EventType)
Catch Ex As Exception
RaiseEvent OnError("Konnte kein Windows-Ereignis schreiben: " & Ex.Message)
End Try
End Sub
End Class
End Namespace