5

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));
Matthew Finlay
  • 3,354
  • 2
  • 27
  • 32

3 Answers3

2

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.

Jon Peterson
  • 723
  • 7
  • 21
  • I like the idea, but when specifying the type in the App.config `type=MyNamespace.MyTextWriterTraceListener` the program crashes. – Matthew Finlay Mar 06 '14 at 22:36
  • Interesting. I made a similar custom listener just the other day and had no problems. The one bit that I had to add into my app.config was `type="MyNamespace.MyTextWriterTraceListener, AssemblyNameContainingListener"`. Also make sure that the assembly containing it is being copied, I had a unit test that referenced it in the app.config but not in the code so Visual Studio was helpful enough to not copy it local. – Jon Peterson Mar 07 '14 at 16:25
  • Thanks, I was leaving off the assembly name. – Matthew Finlay Mar 10 '14 at 02:53
2

I know this doesn't directly answer your question, but use NLog instead. It does more stuff than out-of-the-box diagnostics in terms of logging options and it's really easy to use.

jhsowter
  • 619
  • 3
  • 8
  • 3
    -1 doesn't answer the question at all. OP wasn't asking for alternative logging solutions but rather how to configure the one chosen. – Jon Peterson Mar 06 '14 at 17:42
  • 1
    @JonPeterson I disagree. There is a reasonable chance that this answer will be helpful to somebody, somewhere. – Holf Dec 22 '16 at 12:42
0

Even more simpler, just tell the TestWriterTraceListener to not append the file:

TextWriterTraceListener twtl = new TextWriterTraceListener(new StreamWriter(@"<path to my logfile>", false));

By selecting false the logfile is overwritten each time you initialize the TextWriterTraceListener.

Markus
  • 1
  • Thank you, but I was looking for a solution that would allow specification of the writer in config. Your solution is at the bottom of the question as the behaviour I would like to replicate :) – Matthew Finlay Oct 23 '14 at 09:38