Monday, June 18, 2012

ErrorLogger

ErrorLogging
Try
          
        Catch ex As Exception
            Dim path As String = HostingEnvironment.ApplicationPhysicalPath
            ErrorLogger.CreateInstance().WriteToErrorLog(path, ex.Message, ex.StackTrace, Convert.ToString(ex.InnerException), "Error in ExecuteInitDatabasePackage")
            Throw New FaultException("Error thrown in ExecuteInitDatabasePackage")
        End Try



Imports System.IO
Imports System.Web

Public Class ErrorLogger
    Implements IDisposable

    Private Sub New()

    End Sub
    'Shared dataOpertaion As New DataOperations()
    'Shared readCnfg As New ReadConfig()
    Private Shared log As ErrorLogger

    Public Shared Function CreateInstance() As ErrorLogger
        If (log Is Nothing) Then
            log = New ErrorLogger
        End If

        Return log
    End Function

    Public Sub WriteToErrorLog(ByVal path As String, ByVal msg As String, _
       ByVal stkTrace As String, ByVal innerException As String, ByVal title As String)
        path = path + "ErrorLog\"

        If Not System.IO.Directory.Exists(path) Then
            System.IO.Directory.CreateDirectory(path)
        End If

        'check the file
        Dim fs As FileStream = New FileStream(path + "errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
        Dim s As StreamWriter = New StreamWriter(fs)
        s.Close()
        fs.Close()

        'log it
        Dim fs1 As FileStream = New FileStream(path + "errlog.txt", FileMode.Append, FileAccess.Write)
        Dim s1 As StreamWriter = New StreamWriter(fs1)
        s1.Write("Title: " & title & vbCrLf)
        s1.Write("Message: " & msg & vbCrLf)
        s1.Write("StackTrace: " & stkTrace & vbCrLf)
        s1.Write("InnerException: " & innerException & vbCrLf)
        s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
        s1.Write("================================================" & vbCrLf)
        s1.Close()
        fs1.Close()

    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

No comments: