473,770 Members | 5,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to handling the file concurrency issue?

I create a component to write the error log as follow, but it seems
sometimes locked by other processes, and pop up the I/O exception.

How can we handling the concurrency issue at file level? Retry accessing the
file if catch error => like waiting and polling.

Thanks!

private void LogError2File(s tring errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFil ePath);

DirectoryInfo oDirInfo = new DirectoryInfo(o FileInfo.Direct oryName);
if(!oDirInfo.Ex ists)

oDirInfo.Create ();

if(!oFileInfo.E xists)

oFileInfo.Creat e();

StreamWriter w = File.AppendText (LogFilePath);

w.Write("\r\nLo g Entry : ");

w.WriteLine("{0 } {1}", DateTime.Now.To LongTimeString( ),

DateTime.Now.To LongDateString( ));

w.WriteLine(" :");

w.WriteLine(" :{0}", errorMessage);

w.WriteLine
("----------------------------------------------------------------");

// Update the underlying file.

w.Flush();

// Close

w.Close();

}
Nov 15 '05 #1
2 4718
Mullin,

Instead of calling AppendText, open the FileStream yourself, allowing it
to be shared for read-only access, but you maintain the write access.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I create a component to write the error log as follow, but it seems
sometimes locked by other processes, and pop up the I/O exception.

How can we handling the concurrency issue at file level? Retry accessing the file if catch error => like waiting and polling.

Thanks!

private void LogError2File(s tring errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFil ePath);

DirectoryInfo oDirInfo = new DirectoryInfo(o FileInfo.Direct oryName);
if(!oDirInfo.Ex ists)

oDirInfo.Create ();

if(!oFileInfo.E xists)

oFileInfo.Creat e();

StreamWriter w = File.AppendText (LogFilePath);

w.Write("\r\nLo g Entry : ");

w.WriteLine("{0 } {1}", DateTime.Now.To LongTimeString( ),

DateTime.Now.To LongDateString( ));

w.WriteLine(" :");

w.WriteLine(" :{0}", errorMessage);

w.WriteLine
("----------------------------------------------------------------");

// Update the underlying file.

w.Flush();

// Close

w.Close();

}

Nov 15 '05 #2
do i need to implement any multiple-producer-consumer pattern at this case?

more than one process/application will write to the error log file.

what's the implementation of commercial software then? like iis log, tomcat
log?

thanks!

regards,
mullin

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:up******** ******@TK2MSFTN GP09.phx.gbl...
Mullin,

Instead of calling AppendText, open the FileStream yourself, allowing it to be shared for read-only access, but you maintain the write access.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I create a component to write the error log as follow, but it seems
sometimes locked by other processes, and pop up the I/O exception.

How can we handling the concurrency issue at file level? Retry accessing

the
file if catch error => like waiting and polling.

Thanks!

private void LogError2File(s tring errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFil ePath);

DirectoryInfo oDirInfo = new DirectoryInfo(o FileInfo.Direct oryName);
if(!oDirInfo.Ex ists)

oDirInfo.Create ();

if(!oFileInfo.E xists)

oFileInfo.Creat e();

StreamWriter w = File.AppendText (LogFilePath);

w.Write("\r\nLo g Entry : ");

w.WriteLine("{0 } {1}", DateTime.Now.To LongTimeString( ),

DateTime.Now.To LongDateString( ));

w.WriteLine(" :");

w.WriteLine(" :{0}", errorMessage);

w.WriteLine
("----------------------------------------------------------------");

// Update the underlying file.

w.Flush();

// Close

w.Close();

}


Nov 15 '05 #3

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

Similar topics

4
31201
by: john | last post by:
How do u guys handle multiple sessions?? i.e, opening different browser windows by running iexplore.exe or clicking IE icons and opening the application. My sessions are mixing up. what i mean is suppose i log in my site using username "test". At this time I set $_SESSION="test". And I use $_SESSION inside my application to print the username. Now if I open another browser & log in with "another test" the session
13
1781
by: darkhorse | last post by:
Hi all, While doing a quite big "set of programs" for a university subject I've found myself in the middle of a problem someone surely has had before, so I'm looking for some help. At one point, I call a python cgi that pickle.load's a file, adds or deletes a registry and dumps the result again in the file. I'm worried that the cgi could be called simultaneously from two or more different computers, thus most probably corrupting the...
3
2449
by: Suzanne | last post by:
Hi All I'm having problems getting my data adapter to throw a concurrency exception with an INSERT command. I want to throw a concurrency exception if an attempt is made to enter a row into tb_table when a row with the same int_UID already exists in there. Here is my stored procedure: if not exists (select int_UID from tb_table where int_UID = @aint_UID)
2
5125
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
4
3100
by: Robert Schuldenfrei | last post by:
Dear NG, I was about to "improve" concurrency checking with a Timestamp when I discovered that my current code is not working. After about a day of beating my head against the wall, I am turning to the NG in hopes that someone can spot what I am doing wrong. Key to this technique working is the SQL UPDATE statement. It is designed to fail
1
1428
by: Rotsey | last post by:
Hi, I have a Access 2003 talking to SQL 2005 express DB via linked tables. I also have .NET forms application that also talks to the SQL DB. I want to know how to handle concurrency in the .NET app.
2
1970
by: Axel Gallus | last post by:
I am desperately looking for some resources dealing with error handling in c++. Don't get me wrong.... I have very detailed knowledge of exceptions, assertions and invariants, but what is missing it the following: 1.) When developing a commercial application, how does the usage of exceptions and assertions as well as
5
1852
by: John | last post by:
Hi I have developed the following logic to handle db concurrency violations. I just wonder if someone can tell me if it is correct or if I need a different approach.Would love to know how pros handle it. Thanks Regards
7
1811
by: John | last post by:
Hi I have asked this question before but have not received any clear answer. I have a winform app with bound controls via dataadapter/dataset combination.I have a dbconcurrency exception ex. Now I have the following; DataRow = ex.Row DataTable = ex.Row.Table DataColumns = ex.Row.Table.Columns DataSet = ex.row.Table.Dataset
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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
10059
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...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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
8891
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
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
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

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.