Re: Cannot access file, being used by another user(process)

From: Peter Rilling (peter_at_nospam.rilling.net)
Date: 08/04/04


Date: Wed, 4 Aug 2004 08:21:19 -0700

Maybe your logging should be done in a separate thread. You would need to
have a single instance of the file stream and queue any write requests.
That way you are guaranteed that only one thread will write to the log at
any given time.

You don't necessarily want your threads to have control over when they write
because you could have written half a line when another thread becomes
active and begins to write.

"Chris Fink" <ChrisFink@discussions.microsoft.com> wrote in message
news:823424D2-D4C7-4B4F-979A-9E3073BD60D0@microsoft.com...
> I have written a c# library app that writes to a log file. This app must
be able to handle multiple threads, meaning it may receive 10 requests
simultaneously to write to the log file. To Test this, I have spun of 10
threads and called this library app. The problem that I am running into is
file locking. Occasionaly, a thread will attempt to write to the file while
it is open. The exception is System.IO.IOException (Cannot write to file,
being used by another process).
>
> I would like your solutions on how you allow an application dealing with
File IO to deal with the locking issue. For example, how does IIS overcome
this problem when writing to the W3CService logs? IIS writes to the same
file daily (appending), and I am sure that web sites with large hits would
run into the same problem that I am experiencing.
>
> Essentially, what I am in search of is a way when I try to write to the
file, if it is being used, to loop until the file is available to perform a
write....or some other bulletproof method?
>
> Below is some sample code for the library component:
> //create logMMDDYY.txt for each day, if exists, append to it
> FileInfo t = new FileInfo(fileName);
> if (t.Exists)
> {
> try
> {
> lock(this)
> {
> file=t.AppendText();
> }
> }
> catch(System.IO.IOException ex)
> {
> throw new Exception("lock"+ex.Message);
> }
> }
> else
> {
> file=t.CreateText();
> }
>
file.WriteLine("------------------------------------------------------------
---------------------------------------------");
> file.WriteLine("DateTimeStart: " + startTime.ToString());
> file.WriteLine("DateTimeEnd: " + endTime.ToString());
> file.WriteLine("ExecutionTime: " + (endTime - startTime));
> file.WriteLine("CmdType: " + sqlType.ToString());
> file.WriteLine("DataSource: " + dsn.ToString());
> file.WriteLine("QueryFile: " + sqlFile.ToString());
> file.WriteLine("Result: " + status.ToString());
> file.WriteLine("ThreadID: " + AppDomain.GetCurrentThreadId());
> file.WriteLine("SQLSubmitted: ");
> file.WriteLine(sqlText);
> file.Close();
>
> And code from the calling app:
> // test file locking with log
> Thread[] myThread = new Thread[10];
> int i=0;
> for ( i = 0; i < 10; i ++ )
> {
> myThread[i] = new Thread( new ThreadStart(this.submit[this calls the
component]) );
> myThread[i].Start();
> }
>
>
>
>



Relevant Pages

  • Re: OWA issue
    ... Note that the times in the log file are in GMT. Look for ... As you said,there is no record in IIS log file about requests for ... TELNET port 443 to my server in home,it is all working,now i was ...
    (microsoft.public.exchange.admin)
  • Re: Cannot access file, being used by another user(process)
    ... Peter, Can you please provide more guidance on how to queue the write requests? ... > simultaneously to write to the log file. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem disabling all web logging except for 1 Web Publishing
    ... > After you turn off logging for a Web publishing rule, ... >> Version: ISA Server 2004 STD. ... >> dont want anything stored in this log file except requests made to WPR2 ...
    (microsoft.public.isa)
  • Re: URLSCAN makes pages with integrated authentication very slow
    ... Because since I have done this with the server header remove, ... Performance of authenticated requests is the ... >> and without URLScan from your machine. ... > As you can see in the IIS log file, there are a few requests that are ...
    (microsoft.public.inetserver.iis.security)