Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Aug 20

Written by: Steve Gray
8/20/2010 6:05 PM  RssIcon

I’ve blogged on this a lot, but I keep expanding and fine tuning. Here’s the latest version of the Module that I use to log errors. I just drop this as-is into most projects.

The code emails the error, and writes to the application event log. It also pops up a messagebox with an abbreviated error text (the user doesn’t need everything)

Here’s the code that needs to go in app.config:

  <appSettings>
    <add key="emailserver" value="mymailserver"/>
    <add key="emailuser" value=""/>
    <add key="emailpassword" value=""/>
    <add key="emailRecipient" value=""/>
  </appSettings>

 

There’s also code in here to handle saving window geometry (height and width). Call that like this:

    Private Sub ProjectTracking_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'record the position
        If Me.WindowState <> FormWindowState.Minimized Then
            My.Settings("projectTrackingGeometry") = WindowGeometryToString(Me)
        End If
    End Sub
    Private Sub ProjectTracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        restoreWindowGeometry("projectTrackingGeometry", Me)
    End Sub

 

Module Module1
    Public Enum formStateType As Integer
        loading = 0
        clean = 1
        populated = 2
        newPending = 3
        dirty = 9
    End Enum
    Sub globalErrorHandler(ByVal ex As Exception)
        'log the error
        Dim strMsg As String = ""
        strMsg += "USER: " & AppUser.UserID & vbCrLf
        strMsg += "EXCEPTION: " + vbCrLf
        strMsg += ex.Message + vbCrLf
        'display the error
        If strMsg.Contains("Illegal address") Then
            Exit Sub
        End If
        strMsg += "GETTYPE: " & ex.GetType().FullName + vbCrLf
        strMsg += "SOURCE: " & ex.Source + vbCrLf
        strMsg += "STACKTRACE: " & Replace(ex.StackTrace, "at ", vbCrLf + "at") & vbCrLf
        strMsg += "TARGETSITE: " + ex.TargetSite.ToString & vbCrLf
        If Not ex.InnerException Is Nothing Then
            strMsg += "INNER EXCEPTION: " & vbCrLf
            strMsg += ex.InnerException.Message + vbCrLf
            strMsg += "   GetType: " & ex.InnerException.GetType().FullName + vbCrLf
            strMsg += "   Source: " & ex.InnerException.Source + vbCrLf
            strMsg += "   StackTrace: " & ex.InnerException.StackTrace + vbCrLf
            strMsg += "   TargetSite: " + ex.InnerException.TargetSite.ToString & vbCrLf
        End If
        If ex.Source = "Validation" Then
            globalMessageBox(ex.Message)
        Else
            'mail the error
            sendMail(strMsg)
            WriteToEventLog(strMsg)
        End If
    End Sub
    Public Function WriteToEventLog(ByVal Entry As String) As Boolean
        Dim appName As String = "Application Error"
        Dim eventType As EventLogEntryType = EventLogEntryType.Error
        Dim logName = "Application"
        Dim objEventLog As New EventLog()
        Try
            'Register the App as an Event Source
            If Not EventLog.SourceExists(appName) Then
                EventLog.CreateEventSource(appName, logName)
            End If
            objEventLog.Source = appName
            'WriteEntry is overloaded; this is one
            'of 10 ways to call it
            objEventLog.WriteEntry(Entry, eventType)
            Return True
        Catch Ex As Exception
            Return False
        End Try
        Return True
    End Function
    Sub globalMessageBox(ByVal strMessage As String)
        MsgBox(strMessage, , "Project Tracking")
    End Sub
    Public Function globalMessageBox(ByVal strMessage As String, ByVal buttons As MsgBoxStyle) As MsgBoxResult
        Return MsgBox(strMessage, buttons, Application.ProductName)
    End Function
    Public Sub sendMail(ByVal strBody As String)
        Try
            Dim strEmailServer As String = System.Configuration.ConfigurationManager.AppSettings("emailServer")
            Dim strEmailUser As String = System.Configuration.ConfigurationManager.AppSettings("emailUser")
            Dim strEmailRecipient As String = System.Configuration.ConfigurationManager.AppSettings("emailRecipient")
            Dim mail As New System.Net.Mail.MailMessage(strEmailUser, strEmailRecipient)
            mail.Subject = "NGB Project Tracking 2 Error"
            mail.Body = strBody
            Dim serv As New System.Net.Mail.SmtpClient(strEmailServer)
            'serv.Credentials = New System.Net.NetworkCredential(strEmailUser, strEmailPassword)
            serv.Send(mail)
        Catch ex As Exception
            globalMessageBox("Failed to send Admin Email")
            'no error handling. we're already in an error loop. If this fails, we're just out of luck
        End Try
    End Sub
    Function WindowGeometryToString(ByVal frm As Form) As String
        Return frm.Top.ToString & "|" & frm.Left & "|" & frm.Height & "|" & frm.Width & "|" & frm.WindowState
    End Function
    Sub RestoreWindowGeometry(ByVal settingsName As String, ByRef frm As Form)
        Dim strGeometry As String
        strGeometry = My.Settings(settingsName)
        If strGeometry = "" Then
            Exit Sub
        End If
        Dim g() As String = Split(strGeometry, "|")
        If g(4) = "2" Then
            frm.WindowState = FormWindowState.Maximized
            Exit Sub
        End If
        frm.Top = g(0)
        frm.Left = g(1)
        frm.Height = g(2)
        frm.Width = g(3)
    End Sub
End Module

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us