Trace.WriteLine doesn't work

Is it normal that Trace.WriteLine doesn't work in anyway, when I run the code with just a few line of Trace.WriteLine, nothing happen even if I add any kind of TraceListener, and what is weird is that the IL is completly empty.

void Main()
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new ConsoleTraceListener());

Trace.WriteLine("adads");
Trace.WriteLine("adads");
Trace.WriteLine("adads");
}

IL

IL_0001: call System.Diagnostics.Trace.get_Listeners
IL_0006: callvirt System.Diagnostics.TraceListenerCollection.Clear
IL_000B: nop
IL_000C: call System.Diagnostics.Trace.get_Listeners
IL_0011: newobj System.Diagnostics.ConsoleTraceListener..ctor
IL_0016: callvirt System.Diagnostics.TraceListenerCollection.Add

As you can see the IL stops just after the adding of the listener.

If I run only some Trace

void Main()
{
Trace.WriteLine("adads");
Trace.WriteLine("adads");
Trace.WriteLine("adads");
}

No IL at all!

It seems like linqpad doesn't like Trace stuff, and removes them all before running the code.

I'm running the version v4.51.03

Thanks for any suggestions.

Comments

  • By default TRACE isn't defined and hence the Trace.WriteLine's are not compiled.

    Try

    #define TRACE

    void Main()
    {
    Trace.WriteLine("adads");
    Trace.WriteLine("adads");
    Trace.WriteLine("adads");
    }

    and LinqPad should output the lines to the result panel.
  • Oh yes, now works! I didn't thought to try define the symbol, because by default in release is defined.

    Thanks again!
  • To save typing, and to get the benefit of LINQPad's output formatting, you can instead call DumpTrace():

    "asdf".DumpTrace()

    As with Trace.WriteLine, output is suppressed unless you define the TRACE symbol.