在我的web.config我有:
<system.diagnostics>
<switches>
<add name="logLevelSwitch" value="1" />
</switches>
</system.diagnostics>
有没有办法可以调用,例如:
System.Diagnostics.TraceSwitch["logLevelSwitch"]获取当前值?
在我的web.config我有:
<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标志添加到编译器命令行。