Redirect Trace output to Console
Asked Answered
C

3

75

Let's say I'm working on a little batch-processing console app in VB.Net. I want to be able to structure the app like this:

Sub WorkerMethod()
   'Do some work
   Trace.WriteLine("Work progress")

   'Do more work
   Trace.WriteLine("Another progress update")

   '...
End Sub


Sub Main()

   'Do any setup, like confirm the user wants to continue or whatever

   WorkerMethod()     

End Sub

Note that I'm using Trace rather than Console for my output. This is because the worker method may be called from elsewhere, or even live in a different assembly, and I want to be able to attach different trace listeners to it. So how can I connect the console to the trace?

I can already do it by defining a simple class (shown below) and adding an instance to the Trace's listeners collection, but I'm wondering if there's a more accepted or built in way to accomplish this:

Public Class ConsoleTrace
    Inherits Diagnostics.TraceListener

    Public Overloads Overrides Sub Write(ByVal message As String)
        Console.Write(message)
    End Sub

    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        Console.WriteLine(message)
    End Sub
End Class
Corrugation answered 13/10, 2008 at 17:19 Comment(0)
P
160

You can add the following to your exe's .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 well, in case you're interested in logging to a file.

Preferable answered 13/10, 2008 at 17:21 Comment(2)
upvote for pointing me to System.Diagnostics.ConsoleTraceListner. I'd somehow missed that. I don't otherwise have an app.config, but I can set this up in code easily enough instead.Corrugation
That works great for .exe's that have config's, but I have a similar situation but with a DLL. I don't want to modify the calling exe's .config file (it's nunit-console.exe). I'll keep looking, but threw you an up-vote anyway as I certainly learned something.Ulrikeulster
M
57

Joel,

You could do this instead of the app config method:

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

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

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

Trace.WriteLine("Howdy");

Trace.Listeners.Remove(listener);

Trace.Close();
Melancholy answered 23/10, 2009 at 15:35 Comment(0)
U
11

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

Easy enough:

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

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

Ulrikeulster answered 14/5, 2009 at 14:59 Comment(1)
I know that this is an old post. But I have a problem with this. Can you please look at my post: #5569130. I'm doing the same thing but with Debug, and I'm getting no results, i.e. the output file is empty. Any ideas?Evaporimeter

© 2022 - 2024 — McMap. All rights reserved.