Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stop a TextWriterTraceListener from appending using app.config?

I'm using System.Diagnostics.TraceSource for logging and one of my listeners is a TextWriterTraceListener. In the tracing primer here it sets this up as follows:

<listeners>
  <add initializeData="output.txt" 
       type="System.Diagnostics.TextWriterTraceListener"
       name="myLocalListener" />
</listeners>

The problem is that this will always append to output.txt. How do you alter this to an overwrite in the config file?

Programmatically the listener I want is a:

new TextWriterTraceListener(new StreamWriter("output.txt", false));
like image 869
Matthew Finlay Avatar asked Oct 09 '22 18:10

Matthew Finlay


1 Answers

The simplest solution is to make your own.

I suggest that you inherit from TextWriterTraceListener and in your constructor set the base Writer to what you proposed: new StreamWriter("output.txt", false).

Some sample code:

public class MyTextWriterTraceListener : TextWriterTraceListener
{
    public MyTextWriterTraceListener(string logFileName)
        : base(logFileName)
    {
        base.Writer = new StreamWriter(logFileName, false);
    }
}

This lets you take the initializeData parameter from a configuration file to specify the name of the file, or specify one if created in code.

like image 101
Jon Peterson Avatar answered Oct 12 '22 02:10

Jon Peterson