2011-01-14 90 views
5

Trace.Listeners和Debug.Listeners共享相同的内部集合,因此我无法向Trace.Listeners添加一个跟踪侦听器,也无法向Debug.Listeners添加一个调试侦听器来区分它们。如何区分自定义跟踪侦听器中的跟踪调试和调试?

我该如何做到这一点?

编辑:

为什么我要做到这一点很简单,因为我wrting一个记录层,我们的应用程序,我想通过跟踪不同的日志出来的系统中调试/跟踪是两个来源日志(还有一些其他来源)我想跟踪。

+0

你想完成什么?你可以展示一些你想写的代码,也许你想要的结果是什么?可能有不同的方式来完成你想要的,而不是你可能想到的。 – wageoghe 2011-01-14 18:53:06

+0

其非常简单,我想记录一切到我的自定义听众。在一个中调试呼叫和在一个中调用呼叫。 – Marcus 2011-01-15 10:43:05

回答

4

[编辑]

我错过了问题的标题部分,你提到这在一个自定义跟踪监听器的情况下。所以,显然你写了(或者想写)一个可以区分Trace.WriteLine和Debug.WriteLine的自定义TraceListener。我认为以下所说的所有内容仍然适用于TraceSources,而不是Trace.WriteLine和Debug.WriteLine。然而,我的答案并不一定回答你的问题,除非说我不认为从TraceListener的Write和WriteLine方法中可能会因为调用Trace.WriteLine vs Debug而最终调用它们.WriteLine。

即使您可以从自定义TraceListener中告知Write或WriteLine调用的最终来源,您仍然不清楚要完成什么。无论您想要完成什么,我都必须相信,如果您在TraceSources的代码中将日志记录用于开始时,将会更容易。

您可以在原始问题中添加一些代码,说明如何编写一些应用程序代码,并添加对Trace.WriteLine和Debug.WriteLine的一些调用。另外,显示自定义TraceListener的一些代码,该代码显示如果您可以区分Trace.WriteLine和Debug.WriteLine,您想要执行的操作。喜欢的东西:

public void WriteLine(string msg) 
{ 
    if (WasWrittenFromTrace) 
    { 
    //Before writing to output, add "TRACE" to front of message 
    WriteToOutput("TRACE: {0}", msg); 
    } 
    else 
    if (WasWrittenFromDebug) 
    { 
    //Before writing to output, add "DEBUG" to front of message 
    WriteToOutput("DEBUG: {0}", msg); 
    } 
} 

[编辑完]

this answer我最近张贴在回答关于使用System.Diagnostics程序问题。

那里有很多信息和答案中有关如何使用System.Diagnostics的链接中的很多信息,重点在于使用TraceSources而不是Trace.WriteLine和Debug.WriteLine。

从你的问题,这听起来像你可能想要写一些像这样的代码:

public void MyFunction(int x, int y) 
{ 
    Trace.WriteLine("Entering MyFunction. x = {0}, y = {1}", x, y); 

    int product = x * y; 

    Debug.WriteLine("product = {0}", product); 

    Trace.WriteLine("Exiting MyFunction"); 
} 

,你会很明显,如跟踪输出去一个的TraceListener和调试输出到另一个的TraceListener 。或者在TraceListener中(也许你会自己写),你想知道给定的Write/WriteLine实际上是一个Trace.Write还是一个Debug.Write。我不认为这是真的可能。

你还想以其他方式控制跟踪和调试输出(也许打开一个和一个关闭?

如果你会使用TraceSources,你可以很容易地控制你的跟踪/日志记录的级别,你可以,如果你想这样做,送一些TraceSources的输出到一个TraceListener的和其他的输出到不同TraceListener(和一些TraceSource甚至可以写入多个TraceListeners)。

因此,使用TraceSources,你可能会写这样的代码:

public class MyClass 
{ 
    //Static variable, so all instances of MyClass will have access to the same instance 
    //of the TraceSource 
    private static readonly TraceSource ts = new TraceSource("MyClass"); 

    public void MyMethod(int x, int y) 
    { 
    ts.TraceEvent(TraceEventType.Information, 0, "Entering MyMethod. x = {0}, y = {1}", x, y); 

    int product = x * y; 

    ts.TraceEvent(TraceEventType.Debug, 0, "Product = {0}", product); 

    ts.TraceEvent(TraceEventType.Information, 0, "Exiting MyMethod."); 
    } 
} 

什么是TraceSource在此示例中的好处是什么?

  1. 在你的app.config,你可以打开“MyClass的” TraceSource或关闭,或将其设置到一定水平。如果将其设置为“调试”,则将编写调试和信息消息。如果您将其设置为“信息”,则只会记录信息信息。如果设置的值高于“信息”,则不会记录该示例中的任何消息。

  2. 在您的app.config中,您可以将“MyClass”的输出发送到一个或多个TraceListeners。如果你有更多的TraceSources(“YourClass”,“HisClass”),每个可以到同一个TraceListener或者每个TraceListener或者它们之间的任何组合。

  3. 在您的app.config中,您可以设置switching和/或filtering,使“MyClass”被指定为转到例如两个TraceListeners。一个TraceListener可以过滤,只记录“调试”消息,另一个可以过滤,只记录“信息”消息。

您可以使用TraceSources做更多的事情。阅读上面的链接以及该帖子中的链接。请参阅我在帖子中提到的Ukadc.Diagnostics项目。 Essential.Diagnostics是另一个为System.Diagnostics提供扩展的项目,并展示了使用System.Diagnostics的一些非常好的示例。 Here is a good example from MSDN about using TraceSources, filters, and TraceListeners

在我看来,如果您尝试使用TraceSources而不是Trace。*和Debug。*来为您的代码添加跟踪/日志记录,那么您会更好。

祝你好运!