In my web.config
I have:
<system.diagnostics>
<switches>
<add name="logLevelSwitch" value="1" />
</switches>
</system.diagnostics>
有没有一种方法可以调用,例如:
System.Diagnostics.TraceSwitch["logLevelSwitch"]
获取当前值?
一旦你定义了开关值web.config
文件中,通过创建一个很容易从您的应用程序中获取该值TraceSwitch同名:
private static TraceSwitch logSwitch = new TraceSwitch("logLevelSwitch",
"This is your logLevelSwitch in the config file");
public static void Main(string[] args)
{
// you can get its properties value then:
Console.WriteLine("Trace switch {0} is configured as {1}",
logSwitch.DisplayName,
logSwitch.Level.ToString());
// and you can use it like this:
if (logSwitch.TraceError)
Trace.WriteLine("This is an error");
// or like this also:
Trace.WriteLineIf(logSwitch.TraceWarning, "This is a warning");
}
此外,为了使其发挥作用,根据文档:
您必须启用跟踪或调试才能使用开关。下列
语法是编译器特定的。如果您使用 C# 以外的编译器或
Visual Basic,请参阅编译器的文档。
要在 C# 中启用调试,请添加/d:DEBUG
编译器命令行的标志
当您编译代码时,或者您可以添加#define DEBUG
到顶部
你的文件。在 Visual Basic 中,添加/d:DEBUG=True
给编译器的标志
命令行。
要在 C# 中启用跟踪,请添加/d:TRACE
标记为
编译代码时的编译器命令行,或添加#define TRACE
到文件的顶部。在 Visual Basic 中,添加/d:TRACE=True
标记到编译器命令行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)