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
the following should work
var cfg=(System.Web.Configuration.CompilationSection) ConfigurationManager.GetSection("system.web/compilation"); if (cfg.Debug) { ... }