[ACCEPTED]-Redirect Trace output to Console-trace

Accepted answer
Score: 159

You can add the following to your exe's 3 .config file.

<?xml version="1.0"?>
<configuration>
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add name="logListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="cat.log" />
                <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

I included the TextWriter as 2 well, in case you're interested in logging 1 to a file.

Score: 57

Joel,

You could do this instead of the app 3 config method:

Trace.Listeners.Add(new ConsoleTraceListener());

or this, if you want to manage 2 adding or removing the listener during the 1 life of the app:

ConsoleTraceListener listener = new ConsoleTraceListener();
Trace.Listeners.Add(listener);

Trace.WriteLine("Howdy");

Trace.Listeners.Remove(listener);

Trace.Close();
Score: 11

Great solution, but I have a situation where 6 I have different dll's being run by the 5 same calling exe, so I don't want to modify 4 the calling exe's .config file. I want each 3 dll to handle it's own alteration of the 2 trace output.

Easy enough:

Stream outResultsFile = File.Create ("output.txt");
var textListener = new TextWriterTraceListener (outResultsFile);
Trace.Listeners.Add (textListener);

This will, of course, output 1 Trace output to the "output.txt" file.

More Related questions