Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To Close Text File or Not To Close

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
Hi,

Im after some advice on how to handle my Text File Operations. I have a Windows Service that writes log information to a text File. Events such as Service Stop / Start, Diagnostic Information etc.

My question is should I open the log file and keep it open while the service is running and just keep adding to it using the Streamwriter methods, then close the file on the Service Stop Event. Or it is best practice to open the file, write the data and then close the file gain and then repeat for each operation, hence not needing to close the file on service stop.

Thanks in advance
Daniel.
 
well...personally, I'm a fan of cleaning up after yourself as soon as possible. But that's just me :)

Also, have you thought about using the Event Viewer to store your information? Makes it easy to view and manage the information and can automatically provide a lot of extra information if you need it.

im in ur stakz, overflowin ur heapz!
 
I would recommend that you open/close the file if you dont have to many writes on that file. You can also use File.AppendAllText method if youre using framework 2.0.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
When you are dealing with writing to a textfile you have to consider performance.

If you're just writing Errors or events that happen every few minutes then I would say open and close right away.

If this is a log that is written to a lot then I would initialize the file in a try statement around your application. In the finally statement you would want to close it before the service stops and manually close the file on the service stop.

try
{
LogWriter.CreateLog("YourPathName");
MyService service = new MyService();
service.Run();
}
catch(Exception ex)
{
LogWriter.WriteLine("Error Occured in Service: " + ex.Message + System.Environment.NewLine + ex.StackTrace;
}
finally
{
LogWriter.Close();
}


Another idea for dealing with closing a file is to implement the IDisposable interface and clean up your unmanaged resources there too.

 
whenever I access the IO (files and db) I open, operate and close. I don't leave them open in case something might happen.

I would also use a logging library instead of rolling my own. log4net, nlog, ent.lib. logger are the first 3 that come to mind.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
if something happens (like a crash or power failure) while the text file is opened, you risk yourself to corrupt the file. On the other hand, the performance can be a problem in case you'll be opening and closing the file all the time.

I agree with macleod1021. You should use the event viewer to monitor this kind of info.

 
i always close and append,
look at what i do:
if (!File.Exists(path))
{
//Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(Convert.ToString(DateTime.Now) + ": " + ex.ToString());
sw.Close();
}
}
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(Convert.ToString(DateTime.Now) + ": " + ex.ToString());
sw.Close();
}
 
Code:
sw.WriteLine(string.Format("{0:F}: {1}", DateTime.Now, ex));
i find this easier to read. also because your using the streamwriter, you don't need to explictly call close. that is part of using

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top