Hello There, Guest!
View New Posts  |  View Today's Posts
Log Class VB.NET

  • 0 Vote(s) - 0 Average


05-27-2012, 06:47 PM #1
blackriderws
Junior Member
**
Posts: 24 Threads:12 Joined: May 2012 Reputation: 2

Log Class VB.NET
Hi all again now I bring you another very useful class, it will be able to create a (.log) file all exceptions that occur in your programs.

Example of use:
Code:
Try
                Using objConn
                    objConn.Open()
....
...

                    objConn.Close()
                End Using
                ' errores  
            Catch ex As MySqlException
                Logger.LogInfo(ex)
                MessageBox.Show(ex.Message)
            Catch ex As Exception
                Logger.LogInfo(ex)
                MsgBox(ex.Message.ToString)
            End Try
Code:
mports System.IO

Public Class Logger
    Private Shared Sub Info(ByVal info As Object)
        'Gets folder & file information of the log file  

        Dim fileName As String = app_logfolder & "\qa_Exceptionlog.log"
        Dim dir As DirectoryInfo = New DirectoryInfo(app_logfolder)

        'Check for existence of logger file            
        If File.Exists(fileName) Then
            Try
                Dim fs As FileStream = New FileStream(fileName, FileMode.Append, FileAccess.Write)
                Dim sw As StreamWriter = New StreamWriter(fs)
                sw.WriteLine(vbCrLf & "--- " + vbCrLf + DateTime.Now + " " + info.ToString)
                sw.Close()
                fs.Close()
            Catch dirEx As DirectoryNotFoundException
                LogInfo(dirEx)
            Catch ex As FileNotFoundException
                LogInfo(ex)
            Catch Ex As Exception
                LogInfo(Ex)
            End Try
        Else
            'If file doesn't exist create one               
            Try
                dir = Directory.CreateDirectory(dir.FullName)
                Dim fileStream As FileStream = File.Create(fileName)
                Dim sw As StreamWriter = New StreamWriter(fileStream)
                sw.WriteLine(vbCrLf & "--- " + vbCrLf + DateTime.Now + info.ToString)
                sw.Close()
                fileStream.Close()
            Catch fileEx As FileNotFoundException
                LogInfo(fileEx)
            Catch dirEx As DirectoryNotFoundException
                LogInfo(dirEx)
            Catch ex As Exception
                LogInfo(ex)
            End Try
        End If
    End Sub
    Public Shared Sub LogInfo(ByVal ex As Exception)
        Try
            'Writes error information to the log file including name of the file, line number & error message description               
            Dim trace As Diagnostics.StackTrace = New Diagnostics.StackTrace(ex, True)
            Dim fileNames As String = trace.GetFrame((trace.FrameCount - 1)).GetFileName()
            Dim lineNumber As Int32 = trace.GetFrame((trace.FrameCount - 1)).GetFileLineNumber()
            Info(vbCrLf + "Error In: " + fileNames + vbCrLf + "Line Number:" + lineNumber.ToString() + vbCrLf + "Error Message: " + ex.Message)
        Catch genEx As Exception
            Info(ex.Message)
        End Try
    End Sub
    Public Shared Sub LogInfo(ByVal message As String)
        Try
            'Write general message to the log file       
            Info("Message: " + message)
        Catch genEx As Exception
            Info(genEx.Message)
        End Try
    End Sub

End Class

05-27-2012, 06:59 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,721 Threads:1,025 Joined: Jun 2011 Reputation: 76

RE: Log Class VB.NET
Small things like this are what define the perception of a professional program over just a simple thrown together type application. If I was focused purely on creating a perfect application, this is something that I would definitely do.

Perhaps throwing your own exceptions as well helps define particular exceptions in more detail and throwing things like that into the logfile. A trick; after you catch and handle an exception with a try, catch block, you can actually throw the same exception again to allow the exception to keep the same call stack.

What's the point of that? If you call this method from another method later in the scope of the application's session, the exception call stack will provide the original exception details from the first thrown exception and arguments.

Code:
try
{
    //Error happens
    throw new ArgumentException("exception detail");
}
catch (ArgumentException ex)
{
    MethodToLogException(ex);
    throw;
}

Using throw by itself there, rethrows the same exception; ArgumentException("exception detail")
This post was last modified: 05-27-2012, 07:02 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - 2018)
Automation Programmer
 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   IRC Client Class KoBE 110 84,425 10-01-2016, 06:52 PM
Last Post: AceInfinity
   Overriding a Class in a DLL Lee Stevens 0 1,230 08-07-2014, 10:25 PM
Last Post: Lee Stevens
   A Less Metro IRC Class anwyla 0 1,407 03-10-2014, 08:14 PM
Last Post: anwyla
   Update Form from Class Thread Lee Stevens 2 2,876 10-13-2013, 05:50 PM
Last Post: Lee Stevens
  Configuration: Separating Concerns with a Settings Base Class Null 1 2,057 07-24-2013, 10:35 AM
Last Post: KoBE


Users browsing this thread: 1 Guest(s)