better routine to write to log file
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 Sub
Code:
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
Re: better routine to write to log file
Hi.This is a component gep13 directed( -ordered :( ) me to use and it does much more than simple log.
http://logging.apache.org/log4net/index.html
Re: better routine to write to log file
Ha ha!
I think "ordered" might be a little harsh, but log4net IMO is very good. If you want some steps on how you can get started with it, take a look here:
http://www.vbforums.com/showthread.php?t=595883
The potential problem that you have with this function is that as your application grows, and more users start using it, you are likely to run into "locking" issues as more and more threads, i.e. sessions, try to write to the file.
Personally, I prefer to hand this work over to a tool that has been tried and tested under tremendous loads. log4net is just one example, there are plenty others.
Gary
Re: better routine to write to log file
Thanks guys. I will check this out. Always nice to hear what others are using/doing.
Re: better routine to write to log file
Good stuff.
Let me know how you get on with log4net. I have been meaning to upload that article, because if you are using the NuGet Package Manager, then log4net is a package that you can install directly, so no need to download, unpack, and add reference etc.
Gary