locked
Do people still use TraceListeners? RRS feed

  • Question

  • I'm going through some C# training classes and learning about TraceListeners and Trace switches.

    It seems like this training is based off of .NET 3.0 or something and I'm wondering if Trace listeners are still being used. For my tracing purposes I just write everything to a log text file using a streamwriter and my function LogErrors()

    Wednesday, August 8, 2012 5:18 PM

Answers

  • TraceListeners provide a lot of flexibility in terms of runtime configuration of how the "listening" occurs.  It's still quite useful, especially if you're trying to attach a listener to a system post-deployment.

    Note that .NET 4.5 also adds EventSource, which makes it simpler to use ETW with .NET (see http://www.infoq.com/news/2012/07/ETW-net)  This has HUGE advantages in that it has a tiny fraction of the overhead of previous mechanisms, so you can leave your tracing in production, release builds, and turn it on using something like PerfView at runtime..


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 8, 2012 5:34 PM
  • One of the main advantages of TraceListener is that you don't have to instantiate it.  You can specify it in configuration (the app.Config file) and it will get created for you.  This lets you change the type of tracing by changing your configuration file, and not recompiling.  See the docs for TraceListener for sample config files: http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 8, 2012 6:44 PM

All replies

  • TraceListeners provide a lot of flexibility in terms of runtime configuration of how the "listening" occurs.  It's still quite useful, especially if you're trying to attach a listener to a system post-deployment.

    Note that .NET 4.5 also adds EventSource, which makes it simpler to use ETW with .NET (see http://www.infoq.com/news/2012/07/ETW-net)  This has HUGE advantages in that it has a tiny fraction of the overhead of previous mechanisms, so you can leave your tracing in production, release builds, and turn it on using something like PerfView at runtime..


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 8, 2012 5:34 PM
  • It is convenient to use it if you want to see the traces in runtime by using DebugView application. I'm working on some embedded stuff and we trace all the debug messages from the boards in such manner.
    Wednesday, August 8, 2012 5:35 PM
  • I use. Well usually tracing is implemented by how complex the application is. So for very complex applications we use some third party logging tools, for moderate complex applications I do use TraceListeners directly or possibly thru Enterprise Library and in simple, small applications I might use my own file or database tracing class.

    Wednesday, August 8, 2012 5:36 PM
  • Do you yourself use TraceListeners? I'm trying to figure out where to define them?

    I have a "main" program as a Program.cs file. But it instantiates an object of my REAL class (which is a project added to the solution in VS 2010). Do I instantiate the TraceListeners in the main class or the constructors of my class?

    And by enabling tracing, will it write out trace events WHENEVER there is a problem or do I have to specify it everywhere?

    I feel I can learn more through this class (I'm trying to figure out the difference between TraceListeners and Switches), but if it's an archaic technique, I don't want to waste time learning more.

    • Edited by Lance Stern Wednesday, August 8, 2012 6:17 PM
    Wednesday, August 8, 2012 6:16 PM
  • One of the main advantages of TraceListener is that you don't have to instantiate it.  You can specify it in configuration (the app.Config file) and it will get created for you.  This lets you change the type of tracing by changing your configuration file, and not recompiling.  See the docs for TraceListener for sample config files: http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 8, 2012 6:44 PM