简体   繁体   English

在C#中将调试消息记录到控制台

[英]logging debug messages to console in c#

I have a very very basic c# question about logging debug-trace messages on console. 我有一个非常基本的C#问题,关于在控制台上记录调试跟踪消息。 For redirecting the trace message to console, we can use 要将跟踪消息重定向到控制台,我们可以使用

    Trace.Listeners.Add(new ConsoleTraceListener());

I was expecting something similar to exist for logging debug message to console. 我期望将类似的调试消息记录到控制台中。

    //Debug.Listeners.Add(new ConsoleDebugListener()); //does not exist
    Debug.Listeners.Add(new ConsoleTraceListener());

so the name does not make a difference or am i missing something. 因此名称不会有所不同,或者我错过了一些东西。

EDIT 编辑

I just wanted to know if something like ConsoleDebugListener or something similar exist or not. 我只是想知道是否存在类似ConsoleDebugListener之类的东西。 it works with trace listener though. 它与跟踪侦听器一起工作。

The ConsoleTraceListener is a class that handles how to write messages coming its way; ConsoleTraceListener是一个类,用于处理如何以自己的方式编写消息。 even though it contains Trace it is not the exclusive output method for the Trace class in the System.Diagnostics namespace. 即使它包含Trace它也不是System.Diagnostics命名空间中Trace类的排他输出方法。

If you want to really find why the ConsoleTraceListener works for both Debug and Trace , it is because both of them use the TraceInternal class internally, which then sends messages to the listeners it contains. 如果您想真正找到ConsoleTraceListener对于DebugTrace都起作用的原因,是因为它们两者都在内部使用TraceInternal类,然后将其发送消息到它包含的侦听器。 Maybe that's a good enough reason for the initial listeners to have been called *Trace* ( EventLogTraceListener , XmlWriterTraceListener , etc...) 也许这就是将初始侦听器称为*Trace*EventLogTraceListenerXmlWriterTraceListener等)的足够充分的理由。

Interestingly, listeners are shared between Debug and Trace since they both return the TraceInternal property. 有趣的是,侦听器在DebugTrace之间共享,因为它们都返回TraceInternal属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM