I have been using the LogIt function for a while and has been working but recently get the error: 'System.Text.EncoderFallbackException: Unable to translate Unicode character \uDBC0 at index 220 to specified code page.' which I believe keeps the log file open and then I get the error: 'System.IO.IOException: The process cannot access the file 'C:\App\logs\feed_log_20110607.txt' because it is being used by another process.'
Question: Will LogItv2 do a better job of closing the connection?
Code:Public Shared Sub LogIt(ByVal logMessage As String, Optional ByVal sDate As String = "") Dim sw As StreamWriter Dim sPath As String = "" Dim sCurrentPath As String = "" sCurrentPath = My.Application.Info.DirectoryPath & "\logs" If My.Computer.FileSystem.DirectoryExists(sCurrentPath) = False Then My.Computer.FileSystem.CreateDirectory(sCurrentPath) End If sPath = My.Application.Info.DirectoryPath & String.Format(ConfigurationManager.AppSettings("LogFileName"), DateTime.Now.ToString("yyyyMMdd")) sw = File.AppendText(sPath) If sDate.Length = 0 Then sw.WriteLine("-{0}", logMessage) Else sw.WriteLine("{0}--{1}", DateTime.Now.ToString("yyyyMMdd-hhmmss"), logMessage) End If sw.Flush() sw.Close() sw = Nothing End SubCode:Public Shared Sub LogItv2(ByVal logMessage As String, Optional ByVal sDate As String = "") Dim sPath As String = My.Application.Info.DirectoryPath & String.Format(ConfigurationManager.AppSettings("LogFileName"), DateTime.Now.ToString("yyyyMMdd")) Dim sCurrentPath As String = My.Application.Info.DirectoryPath & "\logs" If My.Computer.FileSystem.DirectoryExists(sCurrentPath) = False Then My.Computer.FileSystem.CreateDirectory(sCurrentPath) End If Using sw As StreamWriter = New StreamWriter(sPath) If sDate.Length = 0 Then sw.WriteLine("-{0}", logMessage) Else sw.WriteLine("{0}--{1}", DateTime.Now.ToString("yyyyMMdd-hhmmss"), logMessage) End If End Using End Sub




Reply With Quote