Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > logging to a text file in high demand environment

Reply
Thread Tools

logging to a text file in high demand environment

 
 
kpg
Guest
Posts: n/a
 
      04-01-2008
Hi all,

I have a vb asp.net web service where I log user activity to a
text file like this:


Using sw As StreamWriter = New StreamWriter(file, True)
sw.WriteLine(data)
sw.Flush()
sw.Close()
End Using


I'm using the 'using' block becuase after reading about it, it
seems like a good idea to ensure resources are released properly.

Question:

When the web service is called simultaneously by several users do
I need to worry about the file being inaccessible because another
process is writing to it?

If so, what is the best way to do this? In a Try..catch I would assume,
perhaps this:


Try
Using sw As StreamWriter = New StreamWriter(file, True)
sw.WriteLine(data)
sw.Flush()
sw.Close()
End Using
Catch ex as exception
'??? - wait for a few milliseconds and retry?
'give up after several attempts???
End Try

Ideally I would like to 'never give up' but then again I don't want a
race condition either.


Thanks,
kpg
 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      04-01-2008
On 1 Apr, 16:39, kpg <(E-Mail Removed)> wrote:
> Hi all,
>
> I have a vb asp.net web service where I log user activity to a
> text file like this:
>
> Using sw As StreamWriter = New StreamWriter(file, True)
> * sw.WriteLine(data)
> * sw.Flush()
> * sw.Close()
> End Using
>
> I'm using the 'using' block becuase after reading about it, it
> seems like a good idea to ensure resources are released properly.
>
> Question:
>
> When the web service is called simultaneously by several users do
> I need to worry about the file being inaccessible because another
> process is writing to it?
>
> If so, what is the best way to do this? *In a Try..catch I would assume,
> perhaps this:
>
> Try
> * Using sw As StreamWriter = New StreamWriter(file, True)
> * * sw.WriteLine(data)
> * * sw.Flush()
> * * sw.Close()
> * End Using
> Catch ex as exception
> * '??? - wait for a few milliseconds and retry?
> * 'give up after several attempts???
> End Try
>
> Ideally I would like to 'never give up' but then again I don't want a
> race condition either.
>
> Thanks,
> kpg


You could look at using a Mutex object (in the System.Threading
namespace). These are essentially mutually exclusive objects that can
control access to shared resources.

Declare the mutex object as private and shared in the declarations
section of your class:

Private Shared ThreadLock as new Mutex()

Then, in the actual routine you would put something like the
following:

dim sw as StreamWriter
try


if Threadlock.WaitOne() Then
sw = New StreamWriter(file, True)
sw.WriteLine(data)
sw.Flush()
End if

catch ex as exception

finally
'Close the stream writer
If not sw is nothing then sw.Close
'always call the release method, otherwise the mutex will be held in
a locked state
Threadlock.ReleaseMutex()
end try

I've left the Try-Catch block, as whenever you do IO operations, you
should always anticipate an error.

I've also added a Finally block. This will ensure that the code in
this section is always executed.

Hope this helps...

Martin


 
Reply With Quote
 
 
 
 
kpg
Guest
Posts: n/a
 
      04-01-2008

That sounds good. The mutex is declared as shared (which is
static) so it exists independent of an instance of the class.

Does the thread pooling method specified in IIS effect the scope
(may not be the right word) of the mutex?

I'm confused as to how several other users on the system, using
the web service (and the class) will be using the same mutex
when that mutex was not given an explicit name. I see that the
mutex, being shared, has only one copy that exists for all
instances of the class.

I may have just answered my question

Maybe I'm confusing IIS 5.0 thread pooling with IIS 6.0,
In 5.0 there was a thread pool setting that ran everything in
a different thread (isolated, I think). In 6.0 that seems to
have changed. I specify different thread pools for different
types of services. But of course this particular service will
run in its assigned pool, so again I guess it works as you
described - not that the thread pool setting would effect this
anyway, now that I think about it.

cool.

thanks,
kpg
 
Reply With Quote
 
Mick Wilson
Guest
Posts: n/a
 
      04-01-2008
On Apr 1, 11:39 am, kpg <(E-Mail Removed)> wrote:
> When the web service is called simultaneously by several users do
> I need to worry about the file being inaccessible because another
> process is writing to it?
>
> If so, what is the best way to do this?


Why not use the Trace class to do your logging, which is thread safe?

http://msdn2.microsoft.com/en-us/lib...a2(VS.80).aspx
 
Reply With Quote
 
kpg
Guest
Posts: n/a
 
      04-01-2008
Mick Wilson <(E-Mail Removed)> wrote in news:62316b20-c8fa-431b-98f7-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> http://msdn2.microsoft.com/en-us/lib...a2(VS.80).aspx


seems like such a simple concept...

I do this:


Dim myTraceLog As New System.IO.FileStream(sLogFile, _
IO.FileMode.OpenOrCreate)

Dim myListener As New TextWriterTraceListener(myTraceLog)

Trace.WriteLine(Now.ToShortTimeString & "," & data)


....but the Trace statement is never run. That is, it's like its
commented out - the compiler is not seeing it.

I'm running a web service, does that matter?


 
Reply With Quote
 
Mick Wilson
Guest
Posts: n/a
 
      04-01-2008
On Apr 1, 3:01 pm, kpg <(E-Mail Removed)> wrote:
> ...but the Trace statement is never run.


You're almost there. You need to add the listener object to the
collection:

Trace.Listeners.Add(myListener)


 
Reply With Quote
 
George Ter-Saakov
Guest
Posts: n/a
 
      04-01-2008
I believe that you can not log into text file in high demand environment...
Writing to file does not support multithreaded access.... You will mess-up
your log file if you try to write in it from different threads
simultaneously.

documentation says about TextWriterTraceListener "Any instance members are
not guaranteed to be thread safe. "


George


"kpg" <(E-Mail Removed)> wrote in message
news:Xns9A738EABDAF20ipostthereforeiam@207.46.248. 16...
> Mick Wilson <(E-Mail Removed)> wrote in news:62316b20-c8fa-431b-98f7-
> (E-Mail Removed):
>
>> http://msdn2.microsoft.com/en-us/lib...a2(VS.80).aspx

>
> seems like such a simple concept...
>
> I do this:
>
>
> Dim myTraceLog As New System.IO.FileStream(sLogFile, _
> IO.FileMode.OpenOrCreate)
>
> Dim myListener As New TextWriterTraceListener(myTraceLog)
>
> Trace.WriteLine(Now.ToShortTimeString & "," & data)
>
>
> ...but the Trace statement is never run. That is, it's like its
> commented out - the compiler is not seeing it.
>
> I'm running a web service, does that matter?
>
>



 
Reply With Quote
 
Mick Wilson
Guest
Posts: n/a
 
      04-01-2008
On Apr 1, 3:51 pm, "George Ter-Saakov" <(E-Mail Removed)> wrote:
> documentation says about TextWriterTraceListener "Any instance members are
> not guaranteed to be thread safe. "


Does that matter if all of the write activities go through the thread-
safe Trace class methods? You may be right about the disk contention
issues, but I'm curious about what you mentioned with regard to the
TextWriterTraceListener itself because I don't have a lot of
experience dealing with concurrency issues.

Thanks,
Mick
 
Reply With Quote
 
kpg
Guest
Posts: n/a
 
      04-01-2008
Thanks for the input.

Here's what I ended up with. As usual, when I spend too much
time on a piece of code I start embellishing it and have to
stop myself.

The reason I broke out WriteTofile is that I also write data
dumps to a unique filename that will never be in use, so why
go through the trouble of 'locking' it, plus I needed it in
the exception handler for AbandonedMutexException.

WriteToLog builds my date-based file name (simplified here),
then calls the protected WriteToSharedFile routine.

I added a handler and log entry for the AbandonedMutexException,
knowing that if I'm writing to a 'shared' file it must be the
log file. Works for me in this case.

ASP.NET 2.0 documentation says the thread receiving the
AbandonedMutexException now owns the mutex so I can proceed.

I contemplated adding a timeout to the WaitOne, say 1000, but
was not sure how to set the 2nd Boolean argument, plus if it's
locked for that long there must be a problem. Not sure how
likly that is.


Sub WriteToLog(ByVal data As String)
Dim sLogFile As String = "c:\path\myfile.log"
WriteToSharedFile(sLogFile, data)
End Sub

Sub WriteToSharedFile(ByVal file As String, ByVal data As String)
Try
If ThreadLock.WaitOne() Then
WriteToFile(file, data)
End If
Catch ex As AbandonedMutexException
WriteToFile(file, "AbandonedMutexException")
WriteToFile(file, data)
Catch ex As Exception
Finally
ThreadLock.ReleaseMutex()
End Try
End Sub

Sub WriteToFile(ByVal file As String, ByVal data As String)
Dim sw As StreamWriter
Try
sw = New StreamWriter(file, True)
sw.WriteLine(data)
sw.Flush()
Catch ex As Exception
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Sub

kpg
 
Reply With Quote
 
George Ter-Saakov
Guest
Posts: n/a
 
      04-02-2008
If Trace is Thread safe you should be ok......
Apparently it prevents multiple threads from writing at the same time but
then it contradict to "high demand environment"

----------------------------------------------------------------------

Somehow I did not think that they (Microsoft) make Trace a thread safe. I
kind of expected that underlying writer will have to take care of thread
safety.. .What if I am logging into database then I have unnecessary
contention in Trace class since it's still going to prevent multiple threads
from writing at the same time......

---------------------------------------
Actually I always used MS Sql for logging. But it's just because I have not
written application that is not using DB for years....

George.




"Mick Wilson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Apr 1, 3:51 pm, "George Ter-Saakov" <(E-Mail Removed)> wrote:
>> documentation says about TextWriterTraceListener "Any instance members
>> are
>> not guaranteed to be thread safe. "

>
> Does that matter if all of the write activities go through the thread-
> safe Trace class methods? You may be right about the disk contention
> issues, but I'm curious about what you mentioned with regard to the
> TextWriterTraceListener itself because I don't have a lot of
> experience dealing with concurrency issues.
>
> Thanks,
> Mick



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logging exception with java.util.logging.Logger#log() to file only Royan Java 1 04-01-2008 09:42 PM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
Re: Logging to a file and closing it again properly (logging module) Christoph Haas Python 1 06-14-2006 08:47 AM
Logging to a file and closing it again properly (logging module) Christoph Haas Python 0 06-12-2006 09:58 PM
[java.util.logging] logging only to _one_ file Stefan Siegl Java 0 08-27-2003 12:29 PM



Advertisments