C# 如何检查 web.config 中是否启用了调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if Debug is enabled in web.config
提问by Dscoduc
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:
我有一些来自 VB.NET 1.1 天的代码,允许我动态检查是否在 web.config 中启用了调试。如果我可以简单地让 Web 管理员启用调试,我想为什么要重新发明轮子来打开/关闭日志记录。这是我在 VB.NET 中使用的代码,效果很好:
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?
当我想将其转换为 C# 并在 .NET 3.5 中使用它时,我遇到了一些麻烦并且无法正常工作。此外,我想使用 ConfigurationManager.GetSection 的较新构造。谁能建议如何最好地阅读system.web/compilation/debug=true|false
价值?
Much appreciated!
非常感激!
采纳答案by driis
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.
此属性实际上查看 web.config 配置设置。如果您使用 Reflector 查看它,您会发现它使用一些内部类获取实际的 ConfigurationSection 对象。
回答by JoshBerke
the following should work
以下应该工作
var cfg=(System.Web.Configuration.CompilationSection) ConfigurationManager.GetSection("system.web/compilation");
if (cfg.Debug)
{
...
}
回答by Stuart.Sklinar
-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.
- 编辑 - 我知道这并没有具体回答这个问题,因为你要求 Web.Config - 它立即建议一个 web.app,而不是在“运行时”决定,但它确实提供了一个好方法检查它是否是调试模式。
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
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)
只需确保DEBUG
在您的项目中打开了常量(见图)