TraceSwitch and SourceSwitch - what's the difference?

爱⌒轻易说出口 提交于 2019-12-05 19:39:56

问题


So I'm probably missing the obvious here, but what actually is the difference between the functionality of the TraceSwitch and SourceSwitch classes?

They both give identical summary descriptions:

Provides a multilevel switch to control tracing and debug output without recompiling your code.

Are the Remarks sections are oddly similar to my eyes. Could someone please clarify the difference in their functionality and usage cases?

(For completeness, I'm using switches with my TraceSource object, rather than the old static Trace class, though I doubt it makes much difference.)


回答1:


The difference is that TraceSwitch works with Trace messages whereas SourceSwitch works with TraceSource messages, which associate the messages with their source. So with a SourceSwitch you can configure your listeners based on on where the trace messages came from.

I agree the documentation doesn't directly point out the difference, but dig around in the related TraceSource class documentation and you'll find this:

The TraceSource class is identified by the name of a source, typically the name of the application. The trace messages coming from a particular component can be initiated by a particular trace source, allowing all messages coming from that component to be easily identified.

There is also a sample showing you how to configure a SourceSwitch to turn off tracing from a trace source.




回答2:


The (older) TraceSwitch basically is a TraceLevel object to be used in combination with the static Trace class.

The (newer) TraceSource combines a TraceLevel concept with actual output methods.

As a consequence, in a large App you can use multiple TraceSwitches to configure Trace settings for different parts (GUI, DAL) of the program but all output will be send to the same TraceListener(s).

With a TraceSource you can have independent output channels. And a slightly better API.




回答3:


This explanation of the difference between TraceLevel and SourceLevel might help:

System.Diagnostics hidden SourceLevels



来源:https://stackoverflow.com/questions/3691749/traceswitch-and-sourceswitch-whats-the-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!