vb to vb.net

bcm

Well-known member
Joined
Aug 13, 2007
Messages
56
Programming Experience
Beginner
I want to show the error occured, date, error description in a text file.
Following is vb code. Can any one tell me how to write it in vb.net?

VB.NET:
Open App.Path & "\ErrorLog" & Replace(Date, "/", "_") & ".txt" For Append As #1
    Write #1, "Error in News update Exe:- Form Load Function ---" & Err.Description & "---" & Time
    Write #1, "--------------------------------"
    Close #1

:confused:
 
Last edited by a moderator:

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
You will need to reference System.IO for the below to work verbatim. Otherwise you'll need to add "System.IO." before Path.Combine and StreamWriter(sFullPath, True) ...

VB.NET:
' the single quotes around the separators in .ToString below force it to always remain an underscore:
Dim sFilename As String = "ErrorLog" & Date.Now.ToString("MM'_'dd'_'yyyy") & ".txt"

' Path.Combine automatically formats our path properly, inserting the backslash when necessary, etc:
Dim sFullPath As String = Path.Combine(Application.ExecutablePath, sFile)

' open sFullPath with Append set to True:
Dim swErrorLog As New StreamWriter(sFullPath, True)
swErrorLog.WriteLine("Error in News update Exe:- Form Load Function ---" & Err.Description & "---" & Date.Now.ToString("hh:mm:ss tt"))
swErrorLog.WriteLine("--------------------------------")
swErrorLog.Close()

If it were me, though, I might set this up as a sub which would accept 'Exception' instead of the now-defunct "Err" object:

VB.NET:
Sub LogError(ex As Exception, source As String)

    ' the single quotes around the separators in .ToString below force it to always remain an underscore:
    Dim sFilename As String = "ErrorLog" & Date.Now.ToString("MM'_'dd'_'yyyy") & ".txt"

    ' Path.Combine automatically formats our path properly, inserting the backslash when necessary, etc:
    Dim sFullPath As String = Path.Combine(Application.ExecutablePath, sFile)

    ' open sFullPath with Append set to True:
    Dim swErrorLog As New StreamWriter(sFullPath, True)
    swErrorLog.WriteLine("Error in News update Exe:- " & source & " ---" & ex.Message & "---" & Date.Now.ToString("hh:mm:ss tt"))
    swErrorLog.WriteLine("--------------------------------")
    swErrorLog.Close()

End Sub

And let's try an example:

VB.NET:
Sub DoDivideByZero()

    Try

        Dim S As Integer = (6 / 0)

    Catch ex As Exception

        ' "System.Reflection.MethodBase.GetCurrentMethod().Name" gets the current sub's name for us
        LogError(ex, System.Reflection.MethodBase.GetCurrentMethod().Name)

    End Try

End Sub
 
Last edited:

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
I try. :) It's more fun to help than write it every day for work (but that's still pretty fun). But yeah there's a lot of "hidden" stuff .NET offers us which VB6 never even came close to. When I first found out about Path.Combine I nearly died. Years of 'If Right(string, 1) = "\" Then ...' sucked big-time. And don't even get me started on GetCurrentMethod; there is so much you can do with that class to help with debugging.

God I'm a nerd...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
You don't need to pass the name of the current method. The Exception object contains all the information you need. The TargetSite property contains the method in which the exception was caught. Also, the ToString method returns a string containing the error message and the stack trace, i.e. the values of the Message and StackTrace properties, plus more.
 

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
Sure, however GetCurrentMethod exposes properties which Exception does not, especially arguments.

These are merely examples which can be custom-fit. I don't intend my code to be final; I write it to give people the opportunity to learn from and customize.
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Sure, however GetCurrentMethod exposes properties which Exception does not, especially arguments.

I'm growing tired of corrections to my code when they are merely examples which can be custom-fit. I don't write my help for final use; I write it to give people the opportunity to learn from and customize.
I just thought a bit more on this and realised that GetCurrentMethod will give you information about the method that caught the exception, while TargetSite will give you information about the method that threw it. The method that threw it is likely to be in the Framework itself. Either or both of those could be useful or not, depending on the circumstances.
 

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
I think there is a way to get a MethodBase (of the TargetSite) off the Exception object; I'll have to look into it this week. This was something I was looking for long ago, when I wanted to log exceptions with all the arguments that were passed to a method.
 
Top Bottom