Why doesn't ConsoleTraceListener write a console?

Quite often, I come across elements in the .NET platform that have few or no code examples on how to use functions. In other cases, there are many examples, but none of them seem to work as prescribed.

Example: class System.Diagnostics.ConsoleTraceListener.

From all that I read, and every example code that I saw when I do something like the following:

ConsoleTraceListner listener = new ConsoleTraceListener();
listener.WriteLine("Yo");

... I should see a console window with "Yo". However, instead of the expected output, it writes "Yo" to the output of Visual Studio, where you can see the usual Trace / Debug messages.

I tried every iteration of the examples that I searched for Googled, including setting up the configuration file for the corresponding listener, adding the listener to the Trace.Listeners collection, custom ConsoleTraceListener types, etc. I even just compiled the version and ran the executable (which does absolutely nothing).

What am I missing?

+3
source share
2 answers

Responding to your comment on Orion Adrian's letter ...

A point ConsoleTraceListenteror any TraceListenerone should not call it directly - it adds it to the trace listener collection so that the rest of your code simply calls Trace.Write, etc. for example

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        ConsoleTraceListener listener = new ConsoleTraceListener();
        Trace.Listeners.Add(listener);

        DoSomething();

        Trace.Flush();
    }

    static void DoSomething()
    {
        DoSomethingElse();
    }

    static void DoSomethingElse()
    {
        // We don't want to have to pass all our logging
        // baggage down this far
        Trace.WriteLine("In DoSomethingElse");
    }
}

TRACE, . "oomph", log4net, - .

+6

TRACE, , , /target:exe, i. . . /target:winexe, , ConsoleTraceListener.

+2

Source: https://habr.com/ru/post/1710276/


All Articles