使用系统有什么好处? Diagnostics.Switch?

What are benefits of using System. Diagnostics.Switch?

我正在尝试进入 system. Diagnostics 命名空间并了解它,但我尝试学习的第一个 class 是 BooleanSwitch

我在 MSDN 上读到了它,但我不明白使用这个库的真正好处是什么。我尝试在互联网上搜索,但我对所看到的单词和句子感到困惑,例如(跟踪,控制调试)。

还有一个名为 Switch 的基础 class,BooleanSwitch 继承自该命名空间中的其他 class。

关于 Diagnostics 命名空间,有什么网站可以让我了解吗?

来自the documentation

A switch provides an efficient mechanism for controlling tracing and debugging output at run time using external settings. The Switch class implements default behavior for switches, allowing you to change the switch level at run time. (emphasis mine - tymtam)

来自同一文档中的示例

// Write a diagnostic message if the switch is set to entering.
Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");

// Insert code to handle processing here...

// Write another diagnostic message if the switch is set to exiting.
Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");

I read about it in MSDN and I can't understand what the benefits of using this library really are.

使用此库的好处是可以轻松获得诊断工具。如果您想使用 .NET 进行一些调试,它绝对是一个值得使用的库。

System.Diagnostics.EventLog

This component provides functionality to write to event logs, read event log entries, and create and delete event logs and event sources on the network.

System.Diagnostics.Trace

Instrumentation allows you to monitor the health of your application running in real-life settings.

System.Diagnostics.StackTrace & System.Diagnostics.StackFrame

These classes will expose the stack information about the routines. Following example will return the name of the calling method.

System.Diagnostics.Debug

Provides a set of methods and properties that help debug your code. This class cannot be inherited. We all are accustomed to this class. Following are the important methods of this class.

System.Diagnostics.Debug.WriteLine <<< this one is big

您可以写入诊断日志,进行跟踪.. 这些对于复杂的项目非常重要。我会说对于更简单的项目来说,它可能有点矫枉过正,但无论如何它绝对是你想要熟悉的东西。

几乎每个图书馆都会在某些情况下提供帮助。图书馆是预先完成的工作,您了解的相关内容越多,您需要做的工作就越少。 MSFT 库通常有很好的文档和设计。

如果您正在寻找优秀的新手指南,这将是一个不错的起点。

这是 VB.NET 但总体概述仍然适用: https://www.codeproject.com/articles/5358/beginning-system-diagnostics

我同意 MSFT 文档通常很难理解和实施。

编辑:我刚刚注意到这是一个 VB.NET(讨厌)link 所以它只对理论和学习方法的作用有用。

布尔开关只是选择是否写入错误信息。这在未引发异常但您的代码注意到数据中存在某些错误或某些其他代码路径已触发您想要写入调试日志的情况下可能很有用。

我认为它不如通用命名空间有用。它可能在某些利基情况下很有用,但与开关无关;无论如何你都会想要学习这个命名空间。

作为,好处是它是一个标准接口。

为了具体解决 SwitchBooleanSwitch,它们是一种定义命名配置值的方法,您可以使用应用程序的 .config 文件进行设置。 您可以根据此类标志​​进行 System.Diagnostic.Trace.WriteLineIf()* 调用(或类似调用),因此它们通常不会执行任何操作,但可以通过更改配置文件 "turned on" 。 (因此,所有诊断功能都将存在于已发布的可执行文件中,但除非您需要,否则将处于非活动状态。)

这与将 Properties.Settings 用于相同目的(正在运行的 .config 文件)非常相似,除了您以不同方式定义可能的属性并且它们进入不同的部分 -- <system.diagnostics> 而不是 <userSettings> - 并且必须手动配置(没有等同于 Properties.Settings.Default.Save() 的这些)。

https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-create-initialize-and-configure-trace-switchesSwitch 类.

的文档更具描述性

*我在这里引用 System.Diagnostic.Trace 而不是 System.Diagnostic.Debug 因为我认为它更适合这个目的。它们大部分相同,除了在编译 Release 可执行文件时将删除对 Debug 方法的调用,而对 Trace 方法的调用将保留但不会执行任何操作,除非您设置在 .config 文件中写入的位置。