.NET Tracing: What is the “Default” listener?

前端 未结 2 1464
北荒
北荒 2021-01-03 19:55

Every example of tracing in .NET people remove the \"Default\" listener:


  
    
         


        
相关标签:
2条回答
  • 2021-01-03 20:32

    It's not clear from that blog post how the code was run, but the DefaultTraceListener is documented like this:

    By default, the Write and WriteLine methods emit the message to the Win32 OutputDebugString function and to the Debugger.Log method. For information about the OutputDebugString function, see the Platform SDK or MSDN.

    So if Debugger.Log is actually printing to a UI window (and quite possibly scrolling it etc) I can see that causing a lot of the slowdown.

    0 讨论(0)
  • 2021-01-03 20:34

    The way Trace Listeners work seems to be a little Automagical because the documentation is not very clear about it. Lets say we have a Business Case for developing a Windows Service. In that service we use Trace.Writeline statements to send information to console or file. The Default Trace Listener sends information to console. If you run the Windows Service and connect with a Debugger to a running process you can see those messages on your console. If you want to change that and send Trace information to a file you can change that in the app.config file like this :

    <system.diagnostics>
        <trace autoflush="false" indentsize="4">
            <listeners>
               <remove name="Default" />
               <add name="Default" type="System.Diagnostics.TextWriterTraceListener" initializeData="ImportServiceOutput.log" />      
            </listeners>
         </trace>
    </system.diagnostics>
    

    Look at the name="Default" that could be any name "myListener" is as good as well. Because the "Default" listener was removed that one will be used instead. Microsoft should document this because this is not clear. In the Microsoft documentation it states that you should use the variable "myListener" then but thats not needed and NOT right anyway because you want declarative where Tracing should go.

    0 讨论(0)
提交回复
热议问题