如何区分自定义跟踪侦听器中的跟踪调用和调试调用?
Trace.Listeners 和 Debug.Listeners 共享相同的内部集合,因此我无法将跟踪侦听器添加到 Trace.Listeners 并将调试侦听器添加到 Debug.Listeners 以区分它们。
我怎样才能实现这个目标?
编辑:
为什么我想这样做只是因为我正在向我们的应用程序写入一个日志记录层,并且我想在整个系统中跟踪不同的日志,其中调试/跟踪是日志的两个来源(还有其他几个来源)来源也)我想跟踪。
Trace.Listeners and Debug.Listeners are sharing the same internal collection thus I cannot add a trace listener to Trace.Listeners and a debug listener to Debug.Listeners to differentiate between them.
How can I achieve this?
Edit:
Why I want to do this is simply because I'm wrting a logging layer to our application and I want to keep track of different logs through out the system where Debug/Trace is two sources of logs (there are a couple of other sources too) I want to keep track of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[编辑]
我错过了问题标题中的部分,您在自定义跟踪侦听器的上下文中提到了这一点。因此,显然您已经编写(或想要编写)一个可以区分 Trace.WriteLine 和 Debug.WriteLine 的自定义 TraceListener。我认为,就 TraceSources 优于 Trace.WriteLine 和 Debug.WriteLine 而言,我下面所说的一切仍然成立。但是,我的答案不一定回答您的问题,只是说我认为不可能从 TraceListener 的 Write 和 WriteLine 方法中判断它们是否由于调用 Trace.WriteLine 与 Debug 而最终被调用.WriteLine。
即使您可以从自定义 TraceListener 中知道调用 Write 或 WriteLine 的最终来源,也不清楚您要实现的目标。无论您想要实现什么目标,我都相信,如果您一开始就将代码中的日志记录语句基于 TraceSources,那么事情会更容易完成。
您能否在原始问题中添加一些代码,以显示如何编写一些应用程序代码,添加一些对 Trace.WriteLine 和 Debug.WriteLine 的调用。另外,显示自定义 TraceListener 中的一些代码,这些代码显示如果您可以区分 Trace.WriteLine 和 Debug.WriteLine,您希望采取什么操作。类似于:
[END EDIT]
请参阅这个答案是我最近发布的,是为了回答有关使用 System.Diagnostics 的问题。
那里有很多信息,并且该答案中的链接中有很多关于如何使用 System.Diagnostics 的信息,重点是使用 TraceSources 而不是 Trace.WriteLine 和 Debug.WriteLine。
从你的问题来看,听起来你可能想编写一些这样的代码:
并且你显然希望跟踪输出转到一个 TraceListener,而调试输出转到另一个 TraceListener。或者在 TraceListener 中(也许您自己编写)您希望能够判断给定的 Write/WriteLine 实际上是 Trace.Write 还是 Debug.Write。我认为这不太可能。
您是否还想以其他方式控制跟踪和调试输出(也许打开一个或关闭一个?
如果您使用 TraceSources,您可以轻松控制跟踪/日志记录的级别,如果您愿意,您可以将某些 TraceSources 的输出发送到一个TraceListener 和其他 TraceListener 的输出(某些 TraceSource 甚至可以写入多个 TraceListener)。
因此,使用 TraceSource,您可以编写如下代码:
TraceSource 有什么好处?
在您的应用程序中, 在 .config 中,您可以打开或关闭“MyClass”TraceSource,或将其设置为某个级别。如果将其设置为“调试”,则将写入调试和信息消息。如果将其设置为“信息”,则仅写入。将记录信息消息。如果您设置高于“信息”,则不会记录示例中的任何消息。
在 app.config 中,您可以将“MyClass”的输出发送到一个或多个 TraceListener。如果您有更多 TraceSource(“YourClass”、“HisClass”),则每个 TraceSource 都可以转到同一个 TraceListener,或者每个可以转到自己的 TraceListener,或者两者之间的任意组合。
在您的 app.config 中,您可以设置切换< /a> 和/或过滤,以便“ MyClass”被指定去例如两个TraceListener。一个 TraceListener 可以进行过滤,以便仅记录“调试”消息,而另一个 TraceListener 可以进行过滤,以便仅记录“信息”消息。
使用 TraceSources 还可以做很多事情。阅读上面的链接以及该帖子中的链接。请参阅我在帖子中提到的 Ukadc.Diagnostics 项目。 Essential.Diagnostics 是另一个项目,它提供了 System.Diagnostics 的扩展,并展示了一些非常好的使用示例系统.诊断。 以下是 MSDN 中关于使用 TraceSources、过滤器和 TraceListener 的一个很好的示例。
在我看来,如果您尝试使用 TraceSources 而不是 Trace.* 和 Debug.* 来向代码添加跟踪/日志记录,您会更好。
祝你好运!
[EDIT]
I missed the part in the title of the question where you mention that this in the context of a custom trace listener. So, apparently you have written (or want to write) a custom TraceListener that could differentiate between Trace.WriteLine and Debug.WriteLine. I think that everything that I say below still holds true as far as TraceSources being preferable to Trace.WriteLine and Debug.WriteLine. However, my answer doesn't necessarily answer your question, except to say that I don't think that it is possible from within a TraceListener's Write and WriteLine methods to tell if they were ultimately invoked because of a call to Trace.WriteLine vs Debug.WriteLine.
It is not clear what you are trying to accomplish, even if you could tell from within the custom TraceListener the ultimate source of call to Write or WriteLine. Whatever it is that you want to accomplish, I have to believe that it would be easier to do if you based your logging statements in your code on TraceSources to begin with.
Could you add some code to your original question that shows how you might write some application code, adding some calls to Trace.WriteLine and Debug.WriteLine. Also, show some code from your custom TraceListener that shows what action you would like to take if you COULD distinguish between Trace.WriteLine and Debug.WriteLine. Something like:
[END EDIT]
See this answer I posted recently in response to a question about using System.Diagnostics.
There is a lot of information there and a lot of information in links within that answer about how to use System.Diagnostics, with an emphasis on using TraceSources rather than Trace.WriteLine and Debug.WriteLine.
From your question, it sounds like you might want to write some code like this:
AND you would apparently like the Trace output to go to one TraceListener and the Debug output to go to another TraceListener. OR within a TraceListener (maybe that you would write yourself) you would like to be able to tell if a given Write/WriteLine is actually a Trace.Write or a Debug.Write. I don't think that is really possible.
Would you also like to control the Trace and Debug output in other ways (maybe turn one on and one off?
If you would use TraceSources, you could easily control the level of your tracing/logging and you could, if you would like to do so, send the output of some TraceSources to one TraceListener and the output of others to a different TraceListener (and some TraceSources could even write to multiple TraceListeners).
So, using TraceSources, you might write code like this:
What are the benefits of TraceSource in this example?
In your app.config you can turn the "MyClass" TraceSource on or off or set it to a certain level. If you set it to "Debug", the Debug and Information messages will be written. If you set it to "Information", only the Information messages will be logged. If you set higher than "Information", none of the messages in the example will be logged.
In your app.config you can send the output of "MyClass" to one or more TraceListeners. If you had more TraceSources ("YourClass", "HisClass"), each could go to the same TraceListener or each could go to its own TraceListener, or any combination in between.
In your app.config you can set up switching and/or filtering such that "MyClass" is designated to go to, for example, two TraceListeners. One TraceListener could filter such that only "Debug" messages are logged and the other could filter such that only "Information" messages are logged.
There is a lot more that you can do with TraceSources. Read the link above and the links within that post. See the Ukadc.Diagnostics project that I refer to in the post. Essential.Diagnostics is another project that provides extensions to System.Diagnostics as well as showing some pretty good examples of using System.Diagnostics. Here is a good example from MSDN about using TraceSources, filters, and TraceListeners.
In my opinion, you will be better off if you try to use TraceSources rather than Trace.* and Debug.* to add tracing/logging to your code.
Good luck!