473,786 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shared textfile...thre ading

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.ReleaseMu tex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFro mThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevelFr omThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' De mutex weer vrijgegven, maw aangeven
dat er geen andere thread meer mee bezig is
End Sub
Dec 7 '06 #1
6 1320
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.ReleaseMu tex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFro mThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevelFr omThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' 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.


"MobileBoy3 6" <Mo*********@gm ail.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.ReleaseMu tex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFro mThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevelFr omThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.FileM ode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' 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.FileS tream(_LogFile, System.IO.FileM ode.Append)

best regards
"Andrew Backer" <aw******@gmail .comschreef in bericht
news:11******** *************@1 6g2000cwy.googl egroups.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.ReleaseM utex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFr omThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevelF romThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.File Mode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.File Mode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' 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
"MobileBoy3 6" <Mo*********@gm ail.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.FileS tream(_LogFile, System.IO.FileM ode.Append)

best regards
"Andrew Backer" <aw******@gmail .comschreef in bericht
news:11******** *************@1 6g2000cwy.googl egroups.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.Release Mutex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelF romThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevel FromThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.Fil eMode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.Fil eMode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' 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
"MobileBoy3 6" <Mo*********@gm ail.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.FileS tream(_LogFile, System.IO.FileM ode.Append)

best regards
"Andrew Backer" <aw******@gmail .comschreef in bericht
news:11******** *************@1 6g2000cwy.googl egroups.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.ReleaseM utex. 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.Threadin g.Mutex
Public Sub New(ByVal Logfile As String, ByVal LoggingLevel As
Integer)
_LogFile = Logfile
_LoggingLevel = LoggingLevel
If _Mutex Is Nothing Then
_Mutex = New System.Threadin g.Mutex
End If
End Sub

Public Sub WriteLog(ByVal Text As String, Optional ByVal
LoggingLevelFr omThisLogging As Integer = 99)
Dim fsOut As System.IO.FileS tream
Dim MyStreamWriter As System.IO.Strea mWriter
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 >= LoggingLevelFro mThisLogging) Or
(LoggingLevelF romThisLogging = 99) Then
' tein meegde loggen
Datum = New DateTime
Datum = Now
If System.IO.File. Exists(_LogFile ) Then
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.File Mode.Append)
Else
fsOut = New System.IO.FileS tream(_LogFile,
System.IO.File Mode.Create)
End If
MyStreamWriter = New System.IO.Strea mWriter(fsOut)
strDatum = Lzero(CStr(Datu m.Day), 2) & "-" & _
Lzero(CStr(Datu m.Month), 2) & "-" & _
CStr(Datum.Year ) & " " & _
Lzero(CStr(Datu m.Hour), 2) & ":" & _
Lzero(CStr(Datu m.Minute), 2) & ":" & _
Lzero(CStr(Datu m.Second), 2) & " : "
Text = strDatum & Text
Text = FilterText(Text )
MyStreamWriter. WriteLine(Text)
MyStreamWriter. Flush()
MyStreamWriter. Close()
fsOut.Close()
End If
_Mutex.ReleaseM utex() ' De mutex weer vrijgegven, maw
aangeven
dat er geen andere thread meer mee bezig is
End Sub
Dec 7 '06 #6
"Robinson" <ro************ **@hotmail.remo ve.this.co.uksc hrieb:
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
5030
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 the file into an array: $textarray = file("textfile.txt); //Using array_rand to pick two random words $rand_numbers = array_rand($textarray, 2);
2
3741
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 something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario? A request is made to...
7
1641
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 some interesting questions. For example... (1). If I have a custom utility class where I keep all of my Shared methods
0
1134
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 following error message on one server I host on.
9
5405
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 for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing shared methods. And, quite honestly, I am very confused. I have tried System.Threading.Monitor.Enter, Synclock,...
18
4568
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 i
8
7680
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) { if(MyBitmap != null) { Graphics g = e.Graphics;
3
1411
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 different processes? Thanks. Amil
15
2786
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 Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.