Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which trace source is used by System.Diagnostics.Trace.WriteLine?

As far as I can see, there are two ways to create trace messages in .NET.

  1. The static methods of System.Diagnostics.Trace:

    Trace.WriteLine("Something happened");
    
  2. The instance methods of System.Diagnostics.TraceSource:

    var ts = new TraceSource("TraceTest");
    ts.TraceInformation("Something happened");
    

In my app.config file, I can either add a trace listener for everything:

<system.diagnostics>
    <trace>
        <listeners>
            ...
        </listeners>
    </trace>
</system.diagnostics>

or for one particular trace source:

<system.diagnostics>
    <sources>
        <source name="...">
            <listeners>
                ...
            </listeners>
        </source>
    </sources>
</system.diagnostics>

My question

If I use the first method (static methods of System.Diagnostics.Trace), which trace source name is used?

I've checked the MSDN page of System.Diagnostics.Trace, but did not find the answer there.

like image 489
Heinzi Avatar asked Oct 18 '22 19:10

Heinzi


1 Answers

I checked the source of Trace.Writeline with JustDecompile and it enums all listeners and sends the message to all:

                foreach (TraceListener listener in TraceInternal.Listeners)
                {
                    if (listener.IsThreadSafe)
                    {
                        listener.WriteLine(message);
                        if (!TraceInternal.AutoFlush)
                        {
                            continue;
                        }
                        listener.Flush();
                    }
                    else
                    {
                        lock (listener)
                        {
                            listener.WriteLine(message);
                            if (TraceInternal.AutoFlush)
                            {
                                listener.Flush();
                            }
                        }
                    }
                }

But forget this ugly Trace calls, use ETW Eventsource for a much better tracing/logging

like image 188
magicandre1981 Avatar answered Nov 15 '22 06:11

magicandre1981