How can I check if I am in a debug or release build in a web application?

In any (non-web) .net project, the compiler automatically declares the DEBUG and TRACE constants, so I can use conditional compilation to, for example, handle exceptions differently in the vs release mode of debugging.

For instance:

#if DEBUG
    /* re-throw the exception... */
#else
    /* write something in the event log... */
#endif

How to get the same behavior in an ASP.net project? It looks like the system.web / compilation section in the web.config file may be what I need, but how to check it programmatically? Or is it better for me to declare the DEBUG constant and comment on it in releases?

EDIT: I'm at VS 2008

+3
source share
3 answers

ConfigurationManager.GetSection() - .. , , #if DEBUG.

#if DEBUG
/* re-throw the exception... */
#else
/* write something in the event log... */
#endif

, , ( , , "" , Builds) - , " DEBUG", .

+5

ontop ,

public bool IsDebugMode
{
  get
  {
#if DEBUG 
    return true;
#else
    return false;
#endif
  }
}
+7

Here is what I did:

protected bool IsDebugMode
{
    get
    {
        System.Web.Configuration.CompilationSection tSection;
        tSection = ConfigurationManager.GetSection("system.web/compilation") as System.Web.Configuration.CompilationSection;
        if (null != tSection)
        {
            return tSection.Debug;
        }
        /* Default to release behavior */
        return false;
    }
}
+4
source

Source: https://habr.com/ru/post/1699045/


All Articles