system.web.compilation.debug与system.codedom.compilers.compiler.compilerOptions/define:Debug = True

osc*_*tin 7 asp.net debugging configuration web-config

当我部署我的ASP.NET web应用程序到生产,我用一个配置变换去除debug="true"<compilation>.但是,就在今天我注意到web.config中的另一个部分如下所示:

<system.codedom>
    <compilers>
        <compiler compilerOptions="/define:Debug=True" />
    </compilers>
</system.codedom>
Run Code Online (Sandbox Code Playgroud)

这是什么?事实上,那是否会破坏将其从中移除的目的<compilation>?如果我删除上面显示的属性会怎样?

Ari*_*tos 6

事实是,这就是为了消除它的目的 <compilation>

来自MSDN C#编译器选项
要打开调试,编译器上的标志就/debug不是了/define:Debug=True

/ debug:指示编译器发出调试信息.
/define:定义预处理程序符号.

因此,当您定义时,Debug=True您只能使这段代码成为现实:

#if DEBUG == true
// Compile what is inside here !
#endif
Run Code Online (Sandbox Code Playgroud)

/define:Debug=True不添加任何额外的调试信息,除非你有包括它们与上面的代码手册.

测试页面

我使用以下代码进行测试,看看发生了什么.

    txtDebug.Text = HttpContext.Current.IsDebuggingEnabled.ToString();

    #if DEBUG
    txtDebug.Text +=  "<br>defined Debug is on";
    #endif
    #if DEBUG == true
    txtDebug.Text +=  "<br>defined Debug = true is on";
    #endif
Run Code Online (Sandbox Code Playgroud)

结果1

现在,如果debug="false"compilerOptions="/define:Debug=True"结果是

false
定义Debug = true已启用

结果2

如果debug="true"compilerOptions="/define:Debug=True"结果是

true
定义Debug在
定义的Debug = true上

结果3

现在我再做一次测试,我在web.config上添加了这一行

  <compiler language="c#;cs;csharp" extension=".cs" 
    compilerOptions="/define:Debug=True /D:DEBUG,TESTFLAG" 
   type="Microsoft.CSharp.CSharpCodeProvider, System, 
     Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       warningLevel="4" />
Run Code Online (Sandbox Code Playgroud)

结果随之而来 debug=false

False(debug为false)
定义Debug已打开(但现在定义的DEBUG正在运行)已
定义Debug = true(这也是运行)
test flag(但定义的额外标志也运行)

MSDN

查看/ define(预处理器定义)MSDN,我看一下声明

/define:Debug=True
Run Code Online (Sandbox Code Playgroud)

只适用于这种代码的情况

#if DEBUG == true
txtDebug.Text +=  "<br>defined Debug = true is on";
#endif
Run Code Online (Sandbox Code Playgroud)