locked
System.Diagnostics Trace/Debug and Custom Trace Listener RRS feed

  • Question

  • Hi,

    Goal: Write custom Trace listener by overriding 'System.Diagnostics.TextWriterTraceListener'.

    Requirement: Output different message based on how message is sent; 'System.Diagnostics.Trace' or 'System.Diagnostics.Debug'.

    Question: from within my Custom Trace Listener how to determine if message was from either of 'System.Diagnostics.Trace' or 'System.Diagnostics.Debug'?

    Thanks,
    Sunday, August 16, 2009 9:40 PM

All replies

  • Hi,

     

     

    Debug and trace share the same Listeners collection, so if you add a listener object to a Debug.Listeners collection in your application, it gets added to the Trace.Listeners collection as well.

    One workaround could be running your code twice to log trace and debug separately by setting complier command flag or macro in your code.  

    Enabling and disabling debug and trace please see below:

        To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

    To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

    Correct me if I’ve misunderstood you.

     

    Cheers,

    Zhe Zhao

    Wednesday, August 19, 2009 6:37 AM
  • Well, I want them to use Debug/Trace I just want to indicate in my log file the category.
    currently, I defined 3 categories: Informational, Error, Debug. I need to be able to trap when Diagnostics.Debug is source of call so I can set category to Debug in my custom TextWriterTraceListener. But how?
    Monday, August 24, 2009 2:55 PM