none
how to detect if calling assembly is debug or release build RRS feed

  • Question

  •  

         I have an assembly (a dll) that is consumed by other assemblies (dll's and exe's), how can I determine if the calling assembly is a debug or a release build?  I have tried the following code, but it does not always work since you can (and I have seen) have release builds set with optimization disabled (I know that is not what you'd normally want, but this has affected the ability of my dll to detect if release or debug build)

    ((System.Diagnostics.DebuggableAttribute)System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), false)[0]).IsJITOptimizerDisabled

     

         To further complicate things, a DEBUG build can also have this optimization enabled, again not practical, but I have had the misfortune of being affected by this setup.

         Is there anything simmilar to the ASP.NET code?

    System.Web.HttpContext.Current.IsDebuggingEnabled


         Any help is appreciated, thanks!

    Wednesday, May 7, 2008 5:46 PM

Answers

  • The notion of a Debug or Release build is specific to Visual Studio.

    As you've noted, a developer can customize build settings to eliminate some or all of the differences between debug and release builds.

     

    What exactly is it you want to know about the calling assembly?

     

    > System.Web.HttpContext.Current.IsDebuggingEnabled

     

    This simply indicates that dynamically compiled ASP.NET code (aspx pages etc) was built in debug mode - as controlled by a web.config setting.  It doesn't for example tell you anything about precompiled code-behind assemblies.

     

     

    Wednesday, May 7, 2008 6:09 PM
  • If you are only interested in examining assemblies you generate yourself, it's easy.  Just add a custom attribute that is different for debug or release builds.  I typically do this by having the following in AssemblyInfo.cs:

     

    Code Snippet
    #if DEBUG
    [assembly: AssemblyConfiguration("Debug build")]
    #else
    [assembly: AssemblyConfiguration("Release build")]
    #endif

     

     

    However I personally wouldn't use this to determine what to log.  I use log4net which gives you lots of configuration options to determine selectively what to log.

     

    Friday, May 9, 2008 5:07 PM

All replies

  • The notion of a Debug or Release build is specific to Visual Studio.

    As you've noted, a developer can customize build settings to eliminate some or all of the differences between debug and release builds.

     

    What exactly is it you want to know about the calling assembly?

     

    > System.Web.HttpContext.Current.IsDebuggingEnabled

     

    This simply indicates that dynamically compiled ASP.NET code (aspx pages etc) was built in debug mode - as controlled by a web.config setting.  It doesn't for example tell you anything about precompiled code-behind assemblies.

     

     

    Wednesday, May 7, 2008 6:09 PM
  •  

     

    Right, I understand that a debug/release build is simply a notion, especially since the developer can modify the debug/optimization variables.

     

    I know I can insert an attribute to mark an assembly as debug or release, but I wanted to be able to determine debug/release builds w/o having to use custom code, and even then, how do I mark an entire assembly as debug/release for a DLL w/o putting this code in every single class?
     

    What I'm looking for is for a way to determine if an assembly is a type X build or a type Y build, the reason is, I have a log class, that handles logging errors and messages from consuming applications, and I want to be able to determine if the consuming assembly is a 'debug' build or a 'release' build and from that determine if to send the log to a dev/test database or a production database, being that I do not want to log all the errors or the extra information that a development/test application would generate, and to be able to differentiate which are actual errors vs. test stuff.

     

    Thanks!

    Friday, May 9, 2008 3:57 PM
  • If you are only interested in examining assemblies you generate yourself, it's easy.  Just add a custom attribute that is different for debug or release builds.  I typically do this by having the following in AssemblyInfo.cs:

     

    Code Snippet
    #if DEBUG
    [assembly: AssemblyConfiguration("Debug build")]
    #else
    [assembly: AssemblyConfiguration("Release build")]
    #endif

     

     

    However I personally wouldn't use this to determine what to log.  I use log4net which gives you lots of configuration options to determine selectively what to log.

     

    Friday, May 9, 2008 5:07 PM
  • yes, i arrived at this same conclussion today...should have read the post a few days back and would have saved myself the time Smile

     

    THANK YOU!
    Wednesday, May 14, 2008 3:14 PM
  • maybe you want to try this:
    System.Diagnostics.Debugger.IsAttached

    • Proposed as answer by KirtsisAth Thursday, June 11, 2009 8:46 PM
    Thursday, June 11, 2009 8:42 PM