user584018
user584018

Reputation: 11304

how to append text into same file for each execution of a program

Below code is writing trace on a text file correctly, but for each program execution (multi-threaded and loop), a different file is generated.

how to make a single file output with all the content. Thanks!

private static void InitiateTracer()  
  {  
    Trace.Listeners.Clear();  
    var dir = AppDomain.CurrentDomain.BaseDirectory;  
    var twtl = new TextWriterTraceListener("log.txt")  
    {  
      Name = "TextLogger",  
      TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime  
    };  
    var ctl = new ConsoleTraceListener(false) { TraceOutputOptions = TraceOptions.DateTime };  
    Trace.Listeners.Add(twtl);  
    Trace.Listeners.Add(ctl);  
    Trace.AutoFlush = true;  
  }  

Upvotes: 0

Views: 337

Answers (1)

ilCosmico
ilCosmico

Reputation: 1449

You can try this

TextWriterTraceListener = new TextWriterTraceListener(new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));

Upvotes: 1

Related Questions