47

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 if I could simply have the web administrator enable debug. Here is the code I used in VB.NET that worked just fine:

ConfigurationSettings.GetConfig("system.web/compilation").Debug.ToString()

When I wanted to convert this to C# and use it in .NET 3.5 I ran into some trouble and it wouldn't work. Additionally, I would like to use the newer construct of ConfigurationManager.GetSection. Can anyone suggest how best to read the system.web/compilation/debug=true|false value?

Much appreciated!

George Kagan
  • 5,913
  • 8
  • 46
  • 50
Dscoduc
  • 7,714
  • 10
  • 42
  • 48

3 Answers3

112

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.

driis
  • 161,458
  • 45
  • 265
  • 341
  • 2
    Great! This is a much better way to check for debug than reading the web.config directly... One thing worth mentioning, I found an article that indicates this method won't take into account if the debug is set at the page level. http://petesbloggerama.blogspot.com/2007/01/is-debug-mode-evil.html – Dscoduc Feb 12 '09 at 19:47
10

the following should work

var cfg=(System.Web.Configuration.CompilationSection) ConfigurationManager.GetSection("system.web/compilation");
if (cfg.Debug)
{
...
}
JoshBerke
  • 66,142
  • 25
  • 126
  • 164
  • 1
    That's a pretty complicated and error prone way to get at it. Should use @driis answer. – dreadwail Apr 26 '12 at 01:08
  • 4
    But what if you're at class library level, where you don't (or shouldn't) have access to the HTTP context? – Stuart.Sklinar Sep 05 '13 at 11:18
  • 2
    @Stuart.Sklinar I aggree, but then again, if you are at class library level you also shouldn't have a reference to the `System.Web` dll. Any suggestions on how to get this without adding the additional library reference? – hofnarwillie Dec 07 '13 at 09:59
  • @Stuart.Sklinar: [continued...] Especially since using `ConfigurationManager.GetSection("system.web/compilation")` assumes a web application? I'm looking to use this in both web and windows apps. – hofnarwillie Dec 07 '13 at 10:07
  • Perhaps you could pass a flag down from your UI level? – Stuart.Sklinar Dec 09 '13 at 11:07
  • 1
    @Stuart.Sklinar Thanks, I actually ended up doing axactly that. :) – hofnarwillie Dec 09 '13 at 17:05
5

-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

Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89