CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 04, 2006

Logger Class for Logging messeges in Files

Public Class Logger
Public Sub PostToLog(ByVal sMessage As String)
Dim dNow As Date = Now
Dim Path As String = System.Configuration.
ConfigurationSettings.AppSettings("LogFilePath")
Dim sFile As String
Dim objWriter As StreamWriter
Dim objReader As StreamReader
Dim sContents As String = ""
  If Path.Substring(Path.Length - 1, 1) = "\" Then            
sFile = Path & "Log -" & Format(dNow.Date, "MM-dd-yy") & ".txt"
Else
sFile = Path & "\Log-" & Format(dNow.Date, "MM-dd-yy") & ".txt"
End If
If System.IO.File.Exists(sFile) Then
Try
objReader = New System.IO.StreamReader(sFile)
sContents = objReader.ReadToEnd()
objReader.Close()
objWriter = New StreamWriter(sFile, False)
objWriter.WriteLine(Format(Now, "hh:mm:ss tt") &
": " & sContents & sMessage)
objWriter.Close()
Catch Ex As Exception
Finally
objWriter.Close()
objReader.Close()
End Try
Else
Try
objWriter = New StreamWriter(sFile, False)
objWriter.WriteLine(Format(Now, "hh:mm:ss tt") &
": " & sMessage)
objWriter.Close()
Catch Ex As Exception
Finally
objWriter.Close()
End Try
End If
End Sub
End Class

Post a Comment