a log file in a console application

maraujo

Member
Joined
Oct 2, 2006
Messages
6
Programming Experience
Beginner
Hi,

I am building a log file and I would like to know if there is a way to write which code line the error has occured.

Example:

"Possible error on line {0}", LINE

Regards,

Marcelo
 
Hey maraujo,
I have a module I use with my console application. I'm assuming your in vb.net, so try this out.

VB.NET:
Imports System.IO
Module LogToFile
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] subLogToFile([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strTextToLog [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'On Error GoTo LocalHandler
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]On[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Error[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Resume[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Variable Declaration and description
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Strings
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strFileLocation [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]'The location and name of the Log File
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Integers
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Boolean
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Date & Time(s)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Arrays
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Objects
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'####################################################################################################
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Variable Definition
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'strFileLocation = App.Path & "/YourFile.txt"
[/COLOR][/SIZE][SIZE=2]strFileLocation = System.AppDomain.CurrentDomain.BaseDirectory & [/SIZE][SIZE=2][COLOR=#800000]"YourFile.txt"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'####################################################################################################
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Code
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Create the file System Object
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objFSO = New FileSystemObject
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Check to see if the log File Exists, if not create it
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(strFileLocation) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'File.CreateTextFile(strFileLocation, False)
[/COLOR][/SIZE][SIZE=2]File.Create(strFileLocation)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Open the log file for Appending
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objTextStream = objFSO.OpenTextFile(strFileLocation, ForAppending)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(strFileLocation, FileMode.OpenOrCreate, FileAccess.ReadWrite)
[/SIZE][SIZE=2][COLOR=#008000]'Log to the file
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objTextStream.WriteLine(Now() & " - " & strTextToLog)
[/COLOR][/SIZE][SIZE=2]Console.WriteLine(Now() & [/SIZE][SIZE=2][COLOR=#800000]" - "[/COLOR][/SIZE][SIZE=2] & strTextToLog)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter(fs)
s.BaseStream.Seek(0, SeekOrigin.End)
s.WriteLine(Now() & [/SIZE][SIZE=2][COLOR=#800000]" - "[/COLOR][/SIZE][SIZE=2] & strTextToLog)
s.Close()
s = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'****vb6 code*****
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objTextStream.Close()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objTextStream = Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'objFSO = Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'****end of vb6 code****
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'####################################################################################################
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Exit the sub before the error handler tag
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Local Handler
[/COLOR][/SIZE][SIZE=2]LocalHandler:
[/SIZE][SIZE=2][COLOR=#008000]'MsgBox (Err.Description & "(" & Err.Source & ")")
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'####################################################################################################
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
 
 
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE]

Usage:
VB.NET:
on error goto LocalHandler
 
LocalHandler:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2][COLOR=#000000] subLogToFile([/COLOR][/SIZE][SIZE=2][COLOR=#800000]"Error On Procedure "[/COLOR][/SIZE][SIZE=2][COLOR=#000000] & strModuleName & [/COLOR][/SIZE][SIZE=2][COLOR=#800000]": Description ~ "[/COLOR][/SIZE][SIZE=2][COLOR=#000000] & Err.Description & [/COLOR][/SIZE][SIZE=2][COLOR=#800000]" On Line #: "[/COLOR][/SIZE][SIZE=2][COLOR=#000000] & Err.Erl & [/COLOR][/SIZE][SIZE=2][COLOR=#800000]" (Source: "[/COLOR][/SIZE][SIZE=2][COLOR=#000000] & Err.Source & [/COLOR][/SIZE][SIZE=2][COLOR=#800000]")"[/COLOR][/SIZE][SIZE=2][COLOR=#000000])[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000][/COLOR][/SIZE] 
[SIZE=2][COLOR=#000000][/COLOR] 
[/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE]

beware, the Err.Erl always returns a ZERO (0) for me, but everything else works well. Hope this helps!
 
Back
Top