VB.Net – Umfangreicher Errorhandler fix und fertig mit Anzeige Logging Mail mit Screenshot uvm.

VB.Net – Umfangreicher Errorhandler fix und fertig mit Anzeige Logging Mail mit Screenshot uvm.

Mit dieser Funktion steht Ihnen ein fix und fertiger Errorhandler zur Verfügung der Ihnen sogar automatisch eine Mail zusendet mit einen Screenshot der Anwendung. Dies ermöglicht eine schnellere Fehleranalyse auch am entfernten Arbeitsplatz.

So wird es gemacht:

Option Strict On
Option Explicit On
 
Module modStart
 
    Public gStruc As IniStructure
    Private mStrModul As String = "modStart"
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub Start_Init()
 
        '----------------------------------------
        Dim strProzedur As String = "Start_Init()"
        '----------------------------------------
 
        Try
 
            '------------------------------------
            With gStruc
                .APP_TITEL = "WinGru"
                .APP_VERSION = "1.0.2"
                .APP_Path = SYS_App_Path() & "\"
                .APP_INI_DATEI = .APP_Path & "system.ini"
 
                .SMTP_HOST = INI_Read("SMTP", "HOST", .APP_INI_DATEI)
                .SMTP_USER = INI_Read("SMTP", "USER", .APP_INI_DATEI)
                '.SMTP_PW = CONV_PW_Read(INI_Read("SMTP", "PASSWORT", .APP_INI_DATEI))
                .SMTP_FROM = INI_Read("SMTP", "FROM", .APP_INI_DATEI)
                .SMTP_SSL = CBool(INI_Read("SMTP", "SMTP_SSL", .APP_INI_DATEI))
 
                .MAIL_TO_1 = INI_Read("MAIL", "TO_1", .APP_INI_DATEI)
                .MAIL_TO_2 = INI_Read("MAIL", "TO_2", .APP_INI_DATEI)
 
                .SQL_Kz = INI_Read("SQL", "SqlKz", .APP_INI_DATEI)
                '.SQL_Pwd = CONV_PW_Read(INI_Read("SQL", "SqlPwd", .APP_INI_DATEI)) 'In-form,6102
                .SQL_Server = INI_Read("SQL", "SqlServer", .APP_INI_DATEI)
                .SQL_DB = INI_Read("SQL", "SqlDB", .APP_INI_DATEI)
 
                .ERR_SILENT = CBool(INI_Read("SYSTEM", "ERR_SILENT", .APP_INI_DATEI))
                .ERR_MAIL = CBool(INI_Read("SYSTEM", "ERR_MAIL", .APP_INI_DATEI))
                .ERR_LOG = CBool(INI_Read("SYSTEM", "ERR_LOG", .APP_INI_DATEI))
 
            End With
            '------------------------------------
 
        Catch ex As Exception
            '------------------------------------
            gStruc.ERR_Message = ex.Message
            gStruc.ERR_Modulname = mStrModul
            gStruc.ERR_ProzedurName = strProzedur
            ERR_Handler(gStruc)
            '------------------------------------
 
        End Try
 
    End Sub
 
End Module
 
'============================================
' VB.NET Beispiel in Visual Basic .Net
' © wilfried.bitz@bridgesolution.de
'============================================
Option Strict On
Option Explicit On
 
Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Text
Imports System.Net.Mail
 
Module modToolbox
 
    '--------------------------------------------
    Private mStrModul As String = "modToolbox"
    Public gSqlCON As New SqlConnection
    '--------------------------------------------
 
    '--------------------------------------------
    'INI Dateien
    Private Declare Ansi Function INI_WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" ( _
                    ByVal lpApplicationName As String, _
                    ByVal lpKeyName As String, _
                    ByVal lpString As String, _
                    ByVal lpFileName As String) As Integer
 
    Private Declare Ansi Function INI_GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" ( _
                    ByVal lpApplicationName As String, _
                    ByVal lpKeyName As String, _
                    ByVal lpDefault As String, _
                    ByVal lpReturnedString As StringBuilder, _
                    ByVal nSize As Integer, _
                    ByVal lpFileName As String) As Integer
    '--------------------------------------------
 
 
    '--------------------------------------------
    Public Structure IniStructure
        Dim SMTP_HOST As String
        Dim SMTP_USER As String
        Dim SMTP_PW As String
        Dim SMTP_FROM As String
        Dim SMTP_SSL As Boolean
        Dim MAIL_TO_1 As String
        Dim MAIL_TO_2 As String
 
        Dim SQL_Kz As String
        Dim SQL_Pwd As String
        Dim SQL_DB As String
        Dim SQL_Server As String
 
        Dim ERR_Modulname As String
        Dim ERR_ProzedurName As String
        Dim ERR_Message As String
        Dim ERR_SILENT As Boolean
        Dim ERR_MAIL As Boolean
        Dim ERR_LOG As Boolean
 
        Dim APP_TITEL As String
        Dim APP_VERSION As String
        Dim APP_Path As String
        Dim APP_INI_DATEI As String
 
        Dim lngSqlUserId As Long
    End Structure
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function INI_Read( _
        ByRef strBereich As String, _
        ByRef strKey As String, _
        ByRef strIniDatei As String) As String
        On Error Resume Next
 
        Dim sb As New StringBuilder(200)
        Dim Ret As Integer = _
            INI_GetPrivateProfileString( _
                strBereich, _
                strKey, _
                "?", _
                sb, _
                200, _
                strIniDatei)
 
        'Return sb.ToString()
        INI_Read = sb.ToString
 
    End Function
 
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub ERR_Handler(ByVal mySTRUC As IniStructure)
 
        Try
 
            '------------------------------------
            If Not mySTRUC.ERR_SILENT Then
                MessageBox.Show(mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message)
            End If
            '------------------------------------
 
            '------------------------------------
            If mySTRUC.ERR_LOG Then
                Log_Add(mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message, mySTRUC)
            End If
            '------------------------------------
 
            '------------------------------------
            If mySTRUC.ERR_MAIL Then
                'Mailbenachrichtigung inkl. Screenshot
                Dim strAttachmments(0) As String
                Dim tmpPic As System.Windows.Forms.PictureBox
                tmpPic = New System.Windows.Forms.PictureBox
                tmpPic.Image = SYS_CaptureScreen()
                tmpPic.Update()
                tmpPic.Image.Save( _
                     mySTRUC.APP_Path & "error_" & Today & ".bmp")
                strAttachmments(0) = mySTRUC.APP_Path & "error_" & Today & ".bmp"
                Dim blnOK As Boolean = MAIL_Send( _
                    mySTRUC.MAIL_TO_1, mySTRUC.MAIL_TO_2, "", _
                    mySTRUC.APP_TITEL & " " & mySTRUC.APP_VERSION, _
                    mySTRUC.APP_TITEL & " " & mySTRUC.APP_VERSION & " - Ein Fehler ist aufgetreten !", _
                    mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message, _
                    strAttachmments, "", mySTRUC)
            End If
            '------------------------------------
 
        Catch ex As Exception
 
        End Try
 
    End Sub
 
   '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub Log_Add( _
        ByVal strText As String, _
        ByVal mySTRUC As IniStructure)
        On Error Resume Next
 
        '----------------------------------------
        Dim strLogDatei As String = _
            mySTRUC.APP_Path & "LOG_" & Today & ".log"
        INI_Write("MESSAGE", CStr(Now), strText, strLogDatei)
        '----------------------------------------
 
        '----------------------------------------
        Exit Sub
        '----------------------------------------
 
    End Sub
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function MAIL_Send( _
        ByVal strTo As String, _
        ByVal strCC As String, _
        ByVal strBCC As String, _
        ByVal strFromDisplay As String, _
        ByVal strSubject As String, _
        ByVal strBody As String, _
        ByVal strAttachments() As String, _
        ByRef strErrMessage As String, _
        ByVal myStruc As IniStructure) As Boolean
        MAIL_Send = False
 
        '----------------------------------------
        Dim strProzedur As String = "MAIL_Send()"
        '----------------------------------------
 
        Try
 
            '------------------------------------
            Dim msgMail = New MailMessage()
            msgMail.To.Add(strTo)
            If strCC <> "" Then msgMail.CC.Add(strCC)
            If strBCC <> "" Then msgMail.Bcc.Add(strBCC)
            msgMail.From = New MailAddress(myStruc.SMTP_FROM, strFromDisplay)
            msgMail.Subject = strSubject
            msgMail.IsBodyHtml = False
            msgMail.Body = strBody
            '------------------------------------
 
            '------------------------------------
            Dim attachment As System.Net.Mail.Attachment
            For i = 0 To UBound(strAttachments)
                If Not IsNothing(strAttachments(i)) Then
                    attachment = New System.Net.Mail.Attachment(strAttachments(i))
                    msgMail.Attachments.Add(attachment)
                End If
            Next
            '------------------------------------
 
            '------------------------------------
            Dim smtp = New SmtpClient()
            smtp.Host = myStruc.SMTP_HOST
            smtp.Credentials = New Net.NetworkCredential(myStruc.SMTP_USER, myStruc.SMTP_PW)
            '------------------------------------
 
            '------------------------------------
            If myStruc.SMTP_SSL Then
                smtp.Port = 587
                smtp.EnableSsl = True
            End If
            '------------------------------------
 
            '------------------------------------
            smtp.Send(msgMail)
            '------------------------------------
 
            '------------------------------------
            MAIL_Send = True
            '------------------------------------
 
            'Catch ex2 As SmtpFailedRecipientException
            '    MsgBox(ex2.Message)
 
            'Catch ex3 As SmtpException
            '    MsgBox(ex3.Message)
 
        Catch ex As Exception
            '------------------------------------
            Dim strTmp As String = "To: " & strTo & " Cc: " & strCC & " From: " & myStruc.SMTP_FROM
            strErrMessage = ex.Message & vbCrLf & vbCrLf & strTmp
            If strErrMessage Like "*Domain not found*" Then 'ungültige Mailadresse
            End If
            myStruc.ERR_Message = strErrMessage
            myStruc.ERR_Modulname = mStrModul
            myStruc.ERR_ProzedurName = strProzedur
            myStruc.ERR_MAIL = False
            ERR_Handler(myStruc)
            myStruc.ERR_MAIL = True
            '------------------------------------
 
        End Try
    End Function
 
End Module

Spendierst du mir eine Tasse Kaffee? Ich würd mich riesig freuen. DANKE !! 🙂

Author: admin

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.