I've a project with which I wants to maintain a log file that if later after release of the product, any issue arise, I can debug the faced issue from this log file. I've one of the solution to write my own text file with writing line at identified positions in the code. But I wants to use some way that is standardized and can capture each and every thing that can be helpful. I've heard about the 'Microsoft Enterprise Libraries' are one of the efficient way to handle log files. can anybody tell me whether these can be more efficient than my own text file and how I can use them in my code easily and efficiently. I've downloaded the library from location Microsoft Enterprise Library Download.

Private Sub errorLog()
Dim path, file As String
Dim nFileNum As Short
Dim Filename As String
Dim dTaskID As Double

Try
If CInt(TextBox1.Text) > 0 Then
MsgBox("worked")
End If
Catch ex As Exception

Filename = "c:\errorlog.txt"
nFileNum = FreeFile()
FileOpen(nFileNum, Filename, OpenMode.Append)
PrintLine(nFileNum, ex.ToString)
FileClose(nFileNum)
path = "C:\WINDOWS\notepad.exe"
file = Filename
dTaskID = Shell(path & " " & file, AppWinStyle.NormalFocus)

End Try

End Sub

You should really write a class that can be reused. Follow the format below and you will be successful in what you want to do.

''' -----------------------------------------------------------------------------
''' <summary>
''' This function will write our error to our log file.
''' </summary>
''' <param name="strData">Message to write to file</param>
''' <param name="dir">Directory to write to</param>
''' <param name="path">Full file path to write to</param>
''' <param name="append">If false the file will be overwritten, If true the file
''' will be appended</param>
''' -----------------------------------------------------------------------------
Public Sub WriteData(ByVal strData As String, ByVal dir As String, ByVal path As String, _
ByVal append As Boolean)
'Check directory first
If Not (Directory.Exists(dir)) Then
Directory.CreateDirectory(dir)
End If

Dim objReader As StreamWriter
Try
objReader = New StreamWriter(path, append)
objReader.Write("Date & Time: " & Now() & vbCrLf & "Message: " & strData & vbCrLf)
objReader.WriteLine(vbCrLf)
objReader.Close()
Catch Ex As Exception
Throw New Exception(Ex.Message)
End Try
End Sub

This article has been dead for over six months. Start a new discussion instead.