473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Shared textfile...threading

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
Dec 7 '06 #1
6 1299
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

MobileBoy36 wrote:
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
Dec 7 '06 #2

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.


"MobileBoy36" <Mo*********@gmail.comwrote in message
news:45***********************@news.skynet.be...
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

Dec 7 '06 #3
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.A ppend)

best regards
"Andrew Backer" <aw******@gmail.comschreef in bericht
news:11*********************@16g2000cwy.googlegrou ps.com...
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

MobileBoy36 wrote:
>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

Dec 7 '06 #4
No example of a tested and thread safe sub textfilewriter??

best regards,
Mobile boy
"MobileBoy36" <Mo*********@gmail.comschreef in bericht
news:45**********************@news.skynet.be...
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.A ppend)

best regards
"Andrew Backer" <aw******@gmail.comschreef in bericht
news:11*********************@16g2000cwy.googlegrou ps.com...
>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

MobileBoy36 wrote:
>>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


Dec 7 '06 #5
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...&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

MobileBoy36 wrote:
No example of a tested and thread safe sub textfilewriter??

best regards,
Mobile boy
"MobileBoy36" <Mo*********@gmail.comschreef in bericht
news:45**********************@news.skynet.be...
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.A ppend)

best regards
"Andrew Backer" <aw******@gmail.comschreef in bericht
news:11*********************@16g2000cwy.googlegrou ps.com...
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

MobileBoy36 wrote:
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
Dec 7 '06 #6
"Robinson" <ro**************@hotmail.remove.this.co.ukschrieb :
Private m_LockingObject as New String = "My Lock"
I'd use 'Private m_Lock As New Object()'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Dec 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
2
by: Paul Wu | last post by:
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using...
7
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What are the implications, (in terms of memory, application footprint, resource use, threading, and so forth), of using Shared methods? These Shared classes raise...
0
by: William LaMartin | last post by:
This code: Private Shared FileLockMutex As System.Threading.Mutex Shared Sub New() FileLockMutex = New System.Threading.Mutex(False, "MyFileLockMutex") End Sub on an aspx page produces the...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
18
by: jess.austin | last post by:
hi, This seems like a difficult question to answer through testing, so I'm hoping that someone will just know... Suppose I have the following generator, g: def f() i = 0 while True: yield...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
3
by: Amil Hanish | last post by:
Is it possible, using static properties, to have a shared object in the GAC that can be accessed by differnet processes? If so, how? If not, how can I have a shared .NET object that can used by...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.