How do I check if Debug is enabled in web.config

前端 未结 3 619
遇见更好的自我
遇见更好的自我 2020-12-25 10:30

I have some code from my VB.NET 1.1 days that allowed me to dynamically check if Debug was enabled in web.config. I figured why re-invent the wheel in turning on/off logging

相关标签:
3条回答
  • 2020-12-25 10:36

    the following should work

    var cfg=(System.Web.Configuration.CompilationSection) ConfigurationManager.GetSection("system.web/compilation");
    if (cfg.Debug)
    {
    ...
    }
    
    0 讨论(0)
  • 2020-12-25 10:41

    -Edit- I'm aware this doesn't specifically answer the question, as you asked for Web.Config - which immediately suggests a web.app, and is not decided at "run-time", but it does allow a good way to check if it's debug mode.

    On another note, you'd not ideally interchanged between debug and release mode on the same app.. -End edit-

    How about using conditional compilation??

    http://msdn.microsoft.com/en-us/library/aa691099(v=vs.71).aspx

    bool isDebuggingEnabled = false
    
    #if debug
       isDebuggingEnabled = true;
    #endif
    

    That surely would make the most sense, and doesn't require any specific references?

    Just make sure the DEBUG Constant is turned on in your project (See picture)

    enter image description here

    0 讨论(0)
  • 2020-12-25 10:56

    Use:

    HttpContext.Current.IsDebuggingEnabled
    

    This property actually looks at the web.config configuration setting. If you look at it using Reflector you will find that it gets the actual ConfigurationSection object using some internal classes.

    0 讨论(0)
提交回复
热议问题