Writing Log for the applications

The code sample is an utility to any application. It helps write logs of activities that an application performs. The function, moreover, is generalized to be used with any application.

An application simply calls WriteLog() providing the message and it's destination as it's parameters. Other details like timing of the log are added automatically to the message.

The code also handles exceptions that may occur while file operations are being performed.

Public Sub WriteLog(ByVal logMessage As String, ByVal vFileName As String)
Dim vFullFilePath As String
vFullFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString & "Log\"
Dim dir As New IO.DirectoryInfo(vFullFilePath)
If dir.Exists = False Then dir.Create()
'Format(Date.Now, "d-MMM-yy")
vFullFilePath = vFullFilePath & vFileName & GetDate() & ".txt"
Dim tWrite As IO.TextWriter = IO.File.AppendText(vFullFilePath)
tWrite.WriteLine(DateTime.Now.ToLongDateString() & vbTab & DateTime.Now.ToLongTimeString() & vbTab & logMessage)
' Update the underlying file.
Catch ex As Exception
End Try
End Sub


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: