Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text using StreamWriter [duplicate]

This is probably a pretty simple question. In C# I'm trying to write a simple method, called my "DebugWrite" method, to write out any exceptions caught within my program to a text file stored locally. My current code only writes a new file every time, using StreamWriter

How do you program it to check if the file already exists, and if so to append to the current text?. IE:

If(~Exist(debug.txt)
{
  Write new debug.txt.
}
else if(exist(debug.txt))
{
  Append new text.
}
like image 750
keynesiancross Avatar asked Jan 13 '12 17:01

keynesiancross


2 Answers

using(StreamWriter writer = new StreamWriter("debug.txt", true))
{
  writer.WriteLine("whatever you text is");
}

The second "true" parameter tells it to append.

http://msdn.microsoft.com/en-us/library/36b035cb.aspx

like image 67
Mike Mooney Avatar answered Oct 23 '22 21:10

Mike Mooney


Also look at log4net, which makes logging to 1 or more event stores — whether it's the console, the Windows event log, a text file, a network pipe, a SQL database, etc. — pretty trivial. You can even filter stuff in its configuration, for instance, so that only log records of a particular severity (say ERROR or FATAL) from a single component or assembly are directed to a particular event store.

http://logging.apache.org/log4net/

like image 1
Nicholas Carey Avatar answered Oct 23 '22 21:10

Nicholas Carey