Sunday, July 3, 2011

Simple Function to write in Text File for error log

First of all you have to import name space
Imports System.IO

then you can write this simple function
Dim FilePath As String = Application.StartupPath + "\Error.log"
Public Sub WriteLog(ByVal StrError As String)
Try
If File.Exists(FilePath) Then
Using Writer As New System.IO.StreamWriter(FilePath, True)
Writer.WriteLine(Now() + "  |  " + StrError)
End Using
Else
Dim fs As FileStream = Nothing
fs = File.Create(FilePath)
Using fs
End Using
Using Writer As New System.IO.StreamWriter(FilePath, True)
Writer.WriteLine(Now() + "  |  " + StrError)
End Using
End If
Catch ex As Exception
End Try
End Sub

No comments:

Post a Comment