Imports System
Imports System.Diagnostics

‘ This class is used for logging to a customeventlog from a SSISPackage Script Taks
‘ By using a ExceptionCategories enum, we can guide the developers to use structured error messages.

‘ [10-JUL-2008 RVL] Created

‘ [RFC] Get standardMessage from *.resx file.
Public Class Logger
    Dim eventLogName As String = "C221A814-192E.SSISPackages.Package1"
    Dim eventLogSource As String = "C221A814-192E.SSISPackages.Package1"
    Dim eventLogMachineName As String = "."
    Enum ExceptionCategories
        UnExpectedError
    End Enum

    ‘Log exception to EventLog and Console
    Public Sub LogExceptionEvent(ByVal message As String, ByVal ex As Exception, ByVal exceptionCategory As ExceptionCategories)
        If String.IsNullOrEmpty(message) Then
            message = ""
        End If

        If Not EventLog.SourceExists(eventLogSource, eventLogMachineName) Then
            EventLog.CreateEventSource(eventLogSource, eventLogName, eventLogMachineName)
        End If

        Dim standardMessage As String
        Dim eventId As Integer
        Dim eventSubCategory As Short = 1
        Select Case exceptionCategory
            Case ExceptionCategories.UnExpectedError
                standardMessage = "Unknow error occured"
                eventId = 1
            Case Else
                standardMessage = "Unknow Enum ExceptionCategories, developer forgot the implement a case statement for this exception category"
                eventId = 1000
        End Select

        Dim exceptionMessage As String = String.Empty
        Dim exceptionStackTrace As String = String.Empty
        Dim innerExceptionMessage As String = String.Empty
        Dim innerExceptionStackTrace As String = String.Empty

        If Not ex Is Nothing Then
            If Not String.IsNullOrEmpty(ex.Message) Then
                exceptionMessage = ex.Message
            End If
            If Not String.IsNullOrEmpty(ex.StackTrace) Then
                exceptionStackTrace = ex.StackTrace
            End If
            If Not ex.InnerException Is Nothing Then
                If Not String.IsNullOrEmpty(ex.InnerException.Message) Then
                    innerExceptionMessage = ex.InnerException.Message
                End If
                If Not String.IsNullOrEmpty(ex.InnerException.StackTrace) Then
                    innerExceptionStackTrace = ex.InnerException.StackTrace
                End If
            End If
        End If

        Dim el As New EventLog(eventLogName, eventLogMachineName, eventLogSource)
        el.WriteEntry("Standard Message [" & standardMessage & "] " _
        & "ExtendedMessage [" & message + "] " _
        & "] ExceptionMessage [" & exceptionMessage + "] " _
        & "] ExceptionStackTrace [" & exceptionStackTrace + "] " _
        & "] InnerExceptionMessage [" & innerExceptionMessage + "] " _
        & "] InnerExceptionStackTrace [" & innerExceptionStackTrace + "] " _
        , EventLogEntryType.Error, eventId, eventSubCategory)
    End Sub
End Class

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.