.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Create Log file in VB.NET

Posted By:Athira Sreejith       Posted Date: March 24, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Imports System.IO
Imports System
Public Class GeneralUtilities
    Dim strError As String = ""
    Dim fileStreamObj As FileStream = Nothing
    Dim streamWriterObj As StreamWriter = Nothing

    Public Sub log(ByVal Request As HttpRequest, ByVal strClassName As String, ByVal strMethodName As String, ByVal strErrorContents As String)
        strError = ""
        Dim fileStreamObj As FileStream = Nothing
        Dim streamWriterObj As StreamWriter = Nothing
        Dim boolFlag As Boolean = False
        Try
            Dim strFileName As String = Request.PhysicalApplicationPath + ConfigurationSettings.AppSettings("LOGPATHFILE")

            boolFlag = checkFile(strFileName)
            If boolFlag Then
                fileStreamObj = New FileStream(strFileName, FileMode.Append, FileAccess.Write)
            Else
                fileStreamObj = New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write)
            End If
            fileStreamObj = New FileStream(strFileName, FileMode.Append, FileAccess.Write)
            streamWriterObj = New StreamWriter(fileStreamObj)
            streamWriterObj.WriteLine("")
            streamWriterObj.WriteLine("class - " + strClassName + " method - " + strMethodName + " Error - " + strErrorContents + " date = " + DateTime.Now)
            streamWriterObj.Flush()
            fileStreamObj.Flush()
        Catch e As Exception
            log("GeneralUtilities", "log", e.ToString())
            strError = e.ToString()
        Finally
            'If (CType(streamWriterObj, Object) <> Nothing) Then
            streamWriterObj = closeStreamWriter(streamWriterObj)
            'End If
            'If (CType(fileStreamObj, Object) <> Nothing) Then
            fileStreamObj = closeFileStream(fileStreamObj)
            'End If
        End Try

    End Sub

    Public Sub log(ByVal strClassName As String, ByVal strMethodName As String, ByVal strErrorContents As String)
        strError = ""
        Dim fileStreamObj As FileStream = Nothing
        Dim streamWriterObj As StreamWriter = Nothing
        Dim boolFlag As Boolean = False
        Try

            Dim strFileName As String = ConfigurationSettings.AppSettings("APPLICATIONPATH") + ConfigurationSettings.AppSettings("LOGPATHFILE")
            boolFlag = checkFile(strFileName)

            Dim intWriteMethod As Integer = convertStringInt(ConfigurationSettings.AppSettings("WRITEMETHOD"))

            If boolFlag Then
                If intWriteMethod = 0 Or intWriteMethod = 1 Then
                    fileStreamObj = New FileStream(strFileName, FileMode.Append, FileAccess.Write)
                Else
                    fileStreamObj = New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write)
                End If
            Else
                fileStreamObj = New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            End If

            streamWriterObj = New StreamWriter(fileStreamObj)
            streamWriterObj.WriteLine("")
            streamWriterObj.WriteLine("class - " + strClassName + " method - " + strMethodName + " Error - " + strErrorContents + " date = " + DateTime.Now)
            streamWriterObj.Flush()
            fileStreamObj.Flush()
        Catch e As Exception
            strError = e.ToString()
            strError = e.Message
            log("GeneralUtilities", "log", e.ToString())

        Finally
            'If (CType(streamWriterObj, Object) <> Nothing) Then
            streamWriterObj = closeStreamWriter(streamWriterObj)
            'End If
            'If (CType(fileStreamObj, Object) <> Nothing) Then
            fileStreamObj = closeFileStream(fileStreamObj)
            'End If
        End Try
    End Sub
    Public Function checkFile(ByVal strFileName As String) As Boolean
        Dim boolFlag As Boolean = File.Exists(strFileName)
        'Dim fileStreamObj As New FileStream(strFileName, FileAccess.Read)
        'boolFlag = True
        'Catch e As Exception
        '    'log("GeneralUtilities", "checkFile", e.ToString())
        '    strError = e.ToString()
        '    boolFlag = False
        'End Try
        Return boolFlag
    End Function

    Public Function closeStreamWriter(ByVal streamWriterObj As StreamWriter) As StreamWriter
        strError = ""
        Try
            streamWriterObj.Close()
            streamWriterObj = Nothing
        Catch e As Exception
            'log("GeneralUtilities", "closeStreamWriter", e.ToString())
            strError = e.ToString()
            streamWriterObj = Nothing
        End Try
        Return streamWriterObj
    End Function

    Public Function closeFileStream(ByVal fileStreamObj As FileStream) As FileStream
        strError = ""
        Try
            fileStreamObj.Close()
            fileStreamObj = Nothing

        Catch e As Exception
            'log("GeneralUtilities", "closeFileStream", e.ToString())
            strError = e.ToString()
            fileStreamObj = Nothing
        End Try
        Return fileStreamObj
    End Function


    Public Function convertNullString(ByVal strString As String) As String
        If strString.ToString.Empty Then
            Return ""
        Else
            Return strString
        End If
    End Function

    Public Function convertStringInt(ByVal strString As String) As Integer
        Dim val As Integer = 0
        Try
            val = CType(strString, Integer)
        Catch e As Exception
            log("UtilClass", "convertStringInt", e.ToString())
            val = 0
            strError = e.ToString()
        End Try
        Return val
    End Function

    Public Function convertStringInt(ByVal strString As String, ByVal intReturnValue As Integer) As Integer
        Dim val As Integer = intReturnValue
        Try
            val = CType(strString, Integer)
        Catch e As Exception
            log("UtilClass", "convertStringInt", e.ToString())
            val = intReturnValue
            strError = e.ToString()
        End Try
        Return val
    End Function

    Public Function addSingleQuotesString(ByVal strString As String) As String
        If strString.ToString.Empty Then
            strString = ""
        End If
        Return "'" + strString + "'"
    End Function

    Public Function addReplaceSingleQuotesString(ByVal strString As String) As String
        If strString.ToString.Empty Then
            strString = ""
        End If
        Return "'" + strString.Replace("'", "''") + "'"
    End Function

End Class

 

Imports System.IO
Public Class FileUtils
    Dim objUtils As New GeneralUtilities()
    Public Sub Log(ByVal strClass As String, ByVal strMethod As String, ByVal strMsg As String)
        Dim boolWriteFlag As Boolean = False
        Try
            boolWriteFlag = Convert.ToBoolean(ConfigurationSettings.AppSettings("WRITELOG"))
        Catch e As Exception
        Finally
            If boolWriteFlag Then
                objUtils.log(strClass, strMethod, strMsg)
            End If
        End Try
    End Sub
End Class



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend