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


Top 5 Contributors of the Month
G N
Akhil Raj
Dhananjay Kumar
laptop charles
Majith

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

 Subscribe to Code Snippets

Create Log file in VB.NET

Posted By :Kajol      Posted Date :24/03/2009   Points :10   Category: ASP.NET Controls    URL: 
 


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



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
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