Lynx
Lynx

Reputation: 257

write text in new line(loop)

i want to create a simple log file for and adding frames.
here is what i found so far.>br> this class invoked from another class which is executed in looping, so everytime the loop end it will call this calss and this class will write a log(data created in time). But, all i got is everytime a new string written in txt file it only replace the first one. how can i add a new string in new line without replacing the old one?
i read the environment.newline but i dont get it.

    private void logFile()
    {
        StreamWriter logfile = null;
        logfile = File.CreateText(Server.MapPath("/CCTV_Files/log.txt"));
        try
        {
            logfile.Write(String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + " Frame added");
        }
        catch (IOException e)
        {
            Console.WriteLine(e);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            if (logfile != null)
            {
                logfile.Close();
            }
        }
     }

Upvotes: 0

Views: 1836

Answers (6)

Nour
Nour

Reputation: 5439

Why bother yourself in opening files and managing them ! Use log4net library: log4net

it is awesome, just few lines of configurations, and you are ready to have log file, full of information and details.

private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

///In your method:
log.Info("Information");
log.Error("Error happened");
log.Debug("bla bla");
///etc.

here is a sample of log file:

176 [main] INFO  examples.Sort - Populating an array of 2 elements in reverse order.
225 [main] INFO  examples.SortAlgo - Entered the sort method.
262 [main] DEBUG SortAlgo.OUTER i=1 - Outer loop.
276 [main] DEBUG SortAlgo.SWAP i=1 j=0 - Swapping intArray[0] = 1 and intArray[1] = 0
290 [main] DEBUG SortAlgo.OUTER i=0 - Outer loop.
304 [main] INFO  SortAlgo.DUMP - Dump of integer array:
317 [main] INFO  SortAlgo.DUMP - Element [0] = 0
331 [main] INFO  SortAlgo.DUMP - Element [1] = 1
343 [main] INFO  examples.Sort - The next log statement should be an error message.
346 [main] ERROR SortAlgo.DUMP - Tried to dump an uninitialized array.
467 [main] INFO  examples.Sort - Exiting main method.

if you insist to use your own way, just use this code:

logfile = File.AppendText(Server.MapPath("/CCTV_Files/log.txt"));

Upvotes: 0

tempidope
tempidope

Reputation: 863

This method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten.

Option 1 : You can use StreamWriter with append as True.

public StreamWriter( string path, bool append )

StreamWriter sw = new StreamWriter(fileName, true)

Option 2: You can also choose to use File.AppendText.

Reference:

http://msdn.microsoft.com/en-us/library/system.io.file.createtext.aspx

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

http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx

Upvotes: 2

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

You need to use File.AppendText(), Here is msdn help

private void logFile()
    {
        StreamWriter logfile = null;
        logfile = File.AppendText(Server.MapPath("/CCTV_Files/log.txt"));
        try
        {
            logfile.Write(String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + " Frame added");
        }
        catch (IOException e)
        {
            Console.WriteLine(e);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            if (logfile != null)
            {
                logfile.Close();
            }
        }
     }

Upvotes: 3

Damir Arh
Damir Arh

Reputation: 17855

Use File.AppendText:

logfile = File.AppendText(Server.MapPath("/CCTV_Files/log.txt"));

instead of File.CreateText:

logfile = File.CreateText(Server.MapPath("/CCTV_Files/log.txt"));

Upvotes: 2

Karthik
Karthik

Reputation: 2399

Try this.

   string path = your file path;
   using(StreamWriter sw = new StreamWriter(path,true))
           {

                    sw.WriteLine("string here");
           }

Upvotes: 1

Akhil
Akhil

Reputation: 1083

You can Open log file in append mod.

http://msdn.microsoft.com/en-us/library/3zc0w663.aspx

and this will be useful

http://roque-patrick.com/windows/final/bbl0190.html

Upvotes: 0

Related Questions