Shared textfile...threading

  • Thread starter Thread starter MobileBoy36
  • Start date Start date
M

MobileBoy36

Hi All,

I want to make a LogFile class that is thread safe. I use a Mutex for it.
But the behavior of the class is not that normal.
In a c# guide I read you can achieve it by simply using Mutex.Waitone and
Mutex.ReleaseMutex. Is that right?
What 's wrong with my sub Writelog then?

Best regards,
Mobileboy



Public Class LogFile


Private _LogFile As String
Private _LoggingLevel As Integer = 99

Private Shared _Mutex As System.Threading.Mutex


Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threading.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFromThisLogging As Integer = 99)
Dim fsOut As System.IO.FileStream
Dim MyStreamWriter As System.IO.StreamWriter
Dim Datum As DateTime
Dim strDatum As String

_Mutex.WaitOne() ' Waitone wacht tot wanneer hij de mutex kan
verwerven, zolang een ander proces hem niet heeft vrij gegeven

If (_LoggingLevel >= LoggingLevelFromThisLogging) Or
(LoggingLevelFromThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File.Exists(_LogFile) Then
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Append)
Else
fsOut = New System.IO.FileStream(_LogFile,
System.IO.FileMode.Create)
End If
MyStreamWriter = New System.IO.StreamWriter(fsOut)
strDatum = Lzero(CStr(Datum.Day), 2) & "-" & _
Lzero(CStr(Datum.Month), 2) & "-" & _
CStr(Datum.Year) & " " & _
Lzero(CStr(Datum.Hour), 2) & ":" & _
Lzero(CStr(Datum.Minute), 2) & ":" & _
Lzero(CStr(Datum.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text)
MyStreamWriter.WriteLine(Text)
MyStreamWriter.Flush()
MyStreamWriter.Close()
fsOut.Close()
End If
_Mutex.ReleaseMutex() ' De mutex weer vrijgegven, maw aangeven
dat er geen andere thread meer mee bezig is
End Sub
 
Synclock:



Private m_LockingObject as New String = "My Lock"



Public Sub WriteLog

SyncLock ( m_LockingObject )

.... do stuff

End SyncLock

End Sub


If there are other methods using the log, then wrap those using the same
locking object as well.
 
Before you go writing something complex, is there a good reason not to
use something like log4net (http://logging.apache.org/log4net/)
instead? It much easier than ms's logging solution, and is safe in
every way you could want.

//Andrew
 
Hi,

Thanks for the answers.
yes I can use a framework for it. But for this case I prefer to use my own
class. I want to know what I am doing wrong.

I know the the existense of synclock.
Using synclock the way you described it seems not to work ( error on lin:
fsOut = New System.IO.FileStream(_LogFile,System.IO.FileMode.Append)

best regards
 
No example of a tested and thread safe sub textfilewriter??

best regards,
Mobile boy
 
Well, if it's really got yer goat I think you could just look at the
source code for log4net?

I always thought that the mutexs = synclock essentially, but here is
some extra locking documentation :
http://everything2.com/index.pl?node_id=1833821&lastnode_id=0

And please post at least some error message. I am not sure you are
actually trying to get help when you just say "it breaks". Please post
some actual error information if you want help debugging.

//Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top