Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. So how do I specify debug only in .NET/C#

So how do I specify debug only in .NET/C#

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestiondebugging
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicknotyet
    wrote on last edited by
    #1

    In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?

    D G J 3 Replies Last reply
    0
    • N nicknotyet

      In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      It's not #ifdef anymore, it's just #if.


      Logifusion[^]

      N 1 Reply Last reply
      0
      • N nicknotyet

        In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Like this:

        #if DEBUG
        Console.WriteLine("Debug version");
        #endif

        From what I can see in MSDN, the calls to Debug methods are not included in the release build (while calls to Trace methods are): "If you use methods in the Debug class to print debugging information and check your logic with assertions, you can make your code more robust without impacting the performance and code size of your shipping product. In Visual Studio 2005 projects, creating a debug build enables Debug. For information on how to disable Debug, see the Visual Studio 2005 documentation. In contrast, in Visual Studio 2005 projects, Trace is enabled by default for both release and debug builds, so code is generated for all trace methods in both release and debug builds. Therefore, you can use Trace to instrument release builds." --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • D Dustin Metzgar

          It's not #ifdef anymore, it's just #if.


          Logifusion[^]

          N Offline
          N Offline
          nicknotyet
          wrote on last edited by
          #4

          Cool, thanks man.

          1 Reply Last reply
          0
          • N nicknotyet

            In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?

            J Offline
            J Offline
            Josh Smith
            wrote on last edited by
            #5

            You should check out the ConditionalAttribute[^], which allows you to cleanly separate conditionally compiled code from the regular code. Josh

            D 1 Reply Last reply
            0
            • J Josh Smith

              You should check out the ConditionalAttribute[^], which allows you to cleanly separate conditionally compiled code from the regular code. Josh

              D Offline
              D Offline
              Dustin Metzgar
              wrote on last edited by
              #6

              Hmm never heard of this before. Have you used it? What concerns me is two things: 1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug and 2. VS will automatically hide a section of code depending on what's defined for the current configuration.  Does this work with the attribute as well?


              Logifusion[^]

              J 1 Reply Last reply
              0
              • D Dustin Metzgar

                Hmm never heard of this before. Have you used it? What concerns me is two things: 1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug and 2. VS will automatically hide a section of code depending on what's defined for the current configuration.  Does this work with the attribute as well?


                Logifusion[^]

                J Offline
                J Offline
                Josh Smith
                wrote on last edited by
                #7

                Dustin Metzgar wrote:

                1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug

                That's the whole point. The attribute allows you to remove debug-only (or whatever-only) code from your "real" code. Since I started using it, I almost never use preprocessor directives anymore because it is just so much cleaner to factor the test/debug code out.

                Dustin Metzgar wrote:

                2. VS will automatically hide a section of code depending on what's defined for the current configuration. Does this work with the attribute as well?

                No, but if you put all of your Conditional methods in one #region, or in a partial class extension, you'll have the same effect. Josh

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups