如何区分自定义跟踪监听器中的跟踪调用和调试调用?

时间:2022-05-28 22:04:05

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.

为什么我想这样做只是因为我写日志层来我们的应用程序,我想通过跟踪不同的日志系统,调试/跟踪两个日志的来源(也有一些其他来源)我想跟踪。

1 个解决方案

#1


4  

[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.

我错过了这个问题的标题,你在一个自定义跟踪监听器的上下文中提到了这个问题。显然,您已经编写(或希望编写)了一个可区分跟踪的自定义TraceListener。WriteLine Debug.WriteLine。我认为我下面所说的一切都是正确的,因为追踪比追踪更可取。WriteLine Debug.WriteLine。然而,我的回答并不一定能回答你的问题,只是说我不认为在TraceListener的写作和WriteLine方法中,如果他们最终被调用,因为有一个追踪的调用,这是不可能的。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.

不清楚您正在尝试实现什么,即使您可以从自定义TraceListener中判断写或写的最终调用源。无论您想要完成什么,我必须相信,如果您首先将日志语句基于跟踪资源放在代码中,那么这将更容易实现。

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:

您是否可以在您的原始问题中添加一些代码,说明如何编写一些应用程序代码,添加一些对Trace的调用。WriteLine Debug.WriteLine。此外,还可以从定制的TraceListener中显示一些代码,这些代码显示了如果您能够区分跟踪,您想要采取什么行动。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);
  }
}

[END EDIT]

(结束编辑)

See this answer I posted recently in response to a question about using System.Diagnostics.

请看我最近在回答关于使用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.

那里有很多信息,在链接中有很多关于如何使用系统的信息。诊断,强调使用跟踪资源而不是跟踪。WriteLine Debug.WriteLine。

From your question, it sounds like you might want to write some code like this:

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

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");
}

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.

显然,您希望跟踪输出到一个TraceListener,调试输出到另一个TraceListener。或者在TraceListener中(您可能会自己编写),您希望能够知道给定的写/写是一个跟踪。写或Debug.Write。我认为这是不可能的。

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).

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

So, using TraceSources, you might write code like this:

因此,使用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.");
  }
}

What are the benefits of TraceSource in this example?

在本例中,TraceSource的好处是什么?

  1. 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.

    在您的app.config中,您可以打开或关闭“MyClass”TraceSource,或者将其设置为某个级别。如果将其设置为“Debug”,则将编写调试和信息消息。如果将其设置为“Information”,则只记录信息消息。如果设置的值高于“信息”,则示例中的消息都不会被记录。

  2. 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.

    在app.config中,您可以将“MyClass”的输出发送给一个或多个tracelistener。如果你有更多的TraceSources(“你的班级”,“他的班级”),每个人都可以使用相同的TraceListener,或者每个人都可以使用它自己的TraceListener,或者两者之间的任何组合。

  3. 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.

    在您的app.config中,您可以设置切换和/或过滤,使“MyClass”被指定到例如两个tracelistener。一个TraceListener可以过滤这样的信息,只有“调试”消息被记录下来,而另一个可以过滤这样的信息,只有“信息”消息被记录下来。

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.

你可以用追踪器做更多的事情。阅读上面的链接和文章中的链接。看到Ukadc。我在文章中提到的诊断项目。必不可少的。诊断是另一个为系统提供扩展的项目。还有一些非常好的使用System.Diagnostics的例子。这是MSDN关于使用TraceSources、filter和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.

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

Good luck!

好运!

#1


4  

[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.

我错过了这个问题的标题,你在一个自定义跟踪监听器的上下文中提到了这个问题。显然,您已经编写(或希望编写)了一个可区分跟踪的自定义TraceListener。WriteLine Debug.WriteLine。我认为我下面所说的一切都是正确的,因为追踪比追踪更可取。WriteLine Debug.WriteLine。然而,我的回答并不一定能回答你的问题,只是说我不认为在TraceListener的写作和WriteLine方法中,如果他们最终被调用,因为有一个追踪的调用,这是不可能的。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.

不清楚您正在尝试实现什么,即使您可以从自定义TraceListener中判断写或写的最终调用源。无论您想要完成什么,我必须相信,如果您首先将日志语句基于跟踪资源放在代码中,那么这将更容易实现。

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:

您是否可以在您的原始问题中添加一些代码,说明如何编写一些应用程序代码,添加一些对Trace的调用。WriteLine Debug.WriteLine。此外,还可以从定制的TraceListener中显示一些代码,这些代码显示了如果您能够区分跟踪,您想要采取什么行动。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);
  }
}

[END EDIT]

(结束编辑)

See this answer I posted recently in response to a question about using System.Diagnostics.

请看我最近在回答关于使用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.

那里有很多信息,在链接中有很多关于如何使用系统的信息。诊断,强调使用跟踪资源而不是跟踪。WriteLine Debug.WriteLine。

From your question, it sounds like you might want to write some code like this:

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

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");
}

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.

显然,您希望跟踪输出到一个TraceListener,调试输出到另一个TraceListener。或者在TraceListener中(您可能会自己编写),您希望能够知道给定的写/写是一个跟踪。写或Debug.Write。我认为这是不可能的。

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).

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

So, using TraceSources, you might write code like this:

因此,使用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.");
  }
}

What are the benefits of TraceSource in this example?

在本例中,TraceSource的好处是什么?

  1. 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.

    在您的app.config中,您可以打开或关闭“MyClass”TraceSource,或者将其设置为某个级别。如果将其设置为“Debug”,则将编写调试和信息消息。如果将其设置为“Information”,则只记录信息消息。如果设置的值高于“信息”,则示例中的消息都不会被记录。

  2. 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.

    在app.config中,您可以将“MyClass”的输出发送给一个或多个tracelistener。如果你有更多的TraceSources(“你的班级”,“他的班级”),每个人都可以使用相同的TraceListener,或者每个人都可以使用它自己的TraceListener,或者两者之间的任何组合。

  3. 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.

    在您的app.config中,您可以设置切换和/或过滤,使“MyClass”被指定到例如两个tracelistener。一个TraceListener可以过滤这样的信息,只有“调试”消息被记录下来,而另一个可以过滤这样的信息,只有“信息”消息被记录下来。

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.

你可以用追踪器做更多的事情。阅读上面的链接和文章中的链接。看到Ukadc。我在文章中提到的诊断项目。必不可少的。诊断是另一个为系统提供扩展的项目。还有一些非常好的使用System.Diagnostics的例子。这是MSDN关于使用TraceSources、filter和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.

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

Good luck!

好运!