48

During compile time I can do a check like

#if DEBUG
    Log("something");
#endif

But what would be the preferred to check if debug="false" is set in Web.config during runtime?

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • When you run the project using Visual studio IDE , then it will ask to modify the web.config file to enable debugging. – Deepak.Aggrawal May 03 '13 at 11:09
  • Why don't you open the web.config file and look for it? – Alberto León May 03 '13 at 11:10
  • The idea is to check once it's been released, that's why the runtime check is important. Sometimes I want to enable debugging in production to get a detailed log of some actions, so if the production server is in debugging mode, it would vomit megabytes of log, but when I disable it it would do normal logging. I realize I can do this with log4net levels as well, but this way I can dynamically create JS files and enable Ajax logging of detailed JavaScript trace messages too! I DON'T want to ajax request to log something just to have log4net ignore it due to level :) – Joel Peltonen May 03 '13 at 13:30
  • 2
    @AlbertoLeón I want the check to be programmatical for fine grained logging checks that I code into files - I will not change code in production. – Joel Peltonen May 03 '13 at 13:33

3 Answers3

83

HttpContext.IsDebuggingEnabled

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcontext.isdebuggingenabled

Madis Otenurm
  • 61
  • 1
  • 7
Britton
  • 2,965
  • 2
  • 22
  • 24
59

In some cases, you may need HttpContext.Current.IsDebuggingEnabled (sort of obvious, but nonetheless)

Vasiliy
  • 948
  • 10
  • 14
  • 8
    I needed to do this today. Perhaps the two answers here should be merged into one? You'll need to use HttpContext.Current if you're in a static method. I'm not sure of the cases when HttpContext by itself is valid. – Bryan Rayner Oct 21 '14 at 13:25
  • @BryanRayner: When you're in a controller, there's often a property available called HttpContext. – StriplingWarrior Aug 17 '18 at 19:09
3

There's another, non-programmatic external and empirical way to check if you've accidentally left the debug=true attribute on system.web/compilation on a Asp.Net website, i.e. to detect if you've left this configuration on in web.config:

 <system.web>
    <compilation debug="true" targetFramework="xxx"/>

By using a tool such as Fiddler, you can intercept a GET request to your web site, and then change this so that to issue a non-Standard DEBUG HTTP Verb to the site, along with an extra Command: stop-debug Header.

  • Select a GET request to your site and drag it into the Composer
  • Switch to the Raw tab (since DEBUG isn't a standard HTTP verb option)
  • Edit the Verb from GET to DEBUG
  • Add an extra Command: stop-debug Header (above the Cookies), but leave the rest of the Headers and Cookies in place
  • Execute the DEBUG command

If the Web site returns 200 and the content OK, then you know you've left debug on. The web site will return 403 - Forbidden if debug is off.

Fiddler Screenshot

If you've got your website building on a CI/CD build pipeline, best way to turn debug off is to add the following XDT in your Web.Release.config

<system.web>
  <compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>

(Turning debug off in Production is a common security audit requirement)

StuartLC
  • 104,537
  • 17
  • 209
  • 285