Re: Cannot access file, being used by another user(process)
From: Chris Fink (ChrisFink_at_discussions.microsoft.com)
Date: 08/04/04
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: what are assembly evidence made for ?"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: global connection string"
- In reply to: Peter Rilling: "Re: Cannot access file, being used by another user(process)"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 4 Aug 2004 08:51:08 -0700
Peter, Can you please provide more guidance on how to queue the write requests?
Just to clarify, since the logging app is a dll, many other apps will be instantiating it at the same time which will cause collision potential with the logging. The dll's actual purpose is the submit sql to a DB, then log the results to a daily log file, just to explain why the logging function is important. I decided to keep the logging in this dll as a black box approach, to ensure that anyone using this sql submitter will be tracked and the results will be logged in a consistent manner for debugging.
Do you recommend that inside this dll I spawn seperate threads for logging? I am not clear on how this would aid in file locking prevention.
Your help is appreciated!
"Peter Rilling" wrote:
> 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();
> > }
> >
> >
> >
> >
>
>
>
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: what are assembly evidence made for ?"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: global connection string"
- In reply to: Peter Rilling: "Re: Cannot access file, being used by another user(process)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|