473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(string errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFilePath);

DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);
if(!oDirInfo.Exists)

oDirInfo.Create();

if(!oFileInfo.Exists)

oFileInfo.Create();

StreamWriter w = File.AppendText(LogFilePath);

w.Write("\r\nLog Entry : ");

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

DateTime.Now.ToLongDateString());

w.WriteLine(" :");

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

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

// Update the underlying file.

w.Flush();

// Close

w.Close();

}
Nov 15 '05 #1
2 4689
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.com

"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2****************@TK2MSFTNGP10.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(string errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFilePath);

DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);
if(!oDirInfo.Exists)

oDirInfo.Create();

if(!oFileInfo.Exists)

oFileInfo.Create();

StreamWriter w = File.AppendText(LogFilePath);

w.Write("\r\nLog Entry : ");

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

DateTime.Now.ToLongDateString());

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.com> wrote in
message news:up**************@TK2MSFTNGP09.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.com

"Mullin Yu" <mu*******@ctil.com> wrote in message
news:%2****************@TK2MSFTNGP10.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(string errorMessage, string LogFilePath)

{

FileInfo oFileInfo = new FileInfo(LogFilePath);

DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);
if(!oDirInfo.Exists)

oDirInfo.Create();

if(!oFileInfo.Exists)

oFileInfo.Create();

StreamWriter w = File.AppendText(LogFilePath);

w.Write("\r\nLog Entry : ");

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

DateTime.Now.ToLongDateString());

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
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...
13
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...
3
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...
2
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...
4
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...
1
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...
2
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...
5
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.