如何以编程方式获取当前的跟踪开关? [英] How can I get the current Trace Switch programatically?
问题描述
在我的 web.config
中,我有:
<system.diagnostics>
<switches>
<add name="logLevelSwitch" value="1" />
</switches>
</system.diagnostics>
有没有一种我可以打电话的方式,例如:
Is there a way that I could call, for example:
System.Diagnostics.TraceSwitch ["logLevelSwitch"]
以获取当前值?
推荐答案
一旦您在 web.config
文件中定义了switch值,就很容易通过创建从应用程序中获取此值 TraceSwitch 代号>
:
Once you've defined the switch value in your web.config
file, it's easy to get this value from your application by creating a TraceSwitch
with the same name:
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");
}
此外,根据文档,此方法可以正常工作
Moreover, for this to work, according to the documentation:
必须启用跟踪或调试才能使用开关.以下语法是特定于编译器的.如果您使用的不是C#或Visual Basic,请参阅您的编译器的文档.
You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.
要启用C#中的调试功能,请在编译器命令行中添加/d:DEBUG
标志编译代码时,也可以在 #define DEBUG
的顶部添加您的文件.在Visual Basic中,将/d:DEBUG = True
标志添加到编译器命令行.
To enabledebugging in C#, add the /d:DEBUG
flag to the compiler command line
when you compile your code, or you can add #define DEBUG
to the top of
your file. In Visual Basic, add the /d:DEBUG=True
flag to the compiler
command line.
要在C#中启用使用跟踪,请将/d:TRACE
标志添加到编译代码或在文件顶部添加 #define TRACE
时使用编译器命令行.在Visual Basic中,添加/d:TRACE = True
标记到编译器命令行.
To enable tracing using in C#, add the /d:TRACE
flag to
the compiler command line when you compile your code, or add #define TRACE
to the top of your file. In Visual Basic, add the /d:TRACE=True
flag to the compiler command line.
这篇关于如何以编程方式获取当前的跟踪开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!