views:

1072

answers:

2

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!

+12  A: 

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
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
+2  A: 

the following should work

var cfg=(System.Web.Configuration.CompilationSection) ConfigurationManager.GetSection("system.web/compilation");
if (cfg.Debug)
{
...
}
JoshBerke
Excellent, thank you.
Dscoduc