如何使用TraceSource跨类 [英] How to use TraceSource across classes

查看:222
本文介绍了如何使用TraceSource跨类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近研究的 TraceSource 文档。 Microsift说TraceSource是一种新的方法,并应被用来代替旧Trace类。

I was recently studying documentation on TraceSource. Microsift says that TraceSource is a new way and should be used instead of old Trace class.

// create single TraceSource instance to be used for logging
static TraceSource ts = new TraceSource("TraceTest");

// somewhere in the code
ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");

现在我的问题。你有大的项目,你有很多类的几个组件。说你想追踪功能的特定位是跨班US $ p $垫。明显的想法是,你需要创建一个特定的TraceSource。

Now my question. You have large project with several assemblies where you have lots of classes. Say you wanna trace specific bit of functionality that is spread across classes. Obvious idea is that you need to create one specific TraceSource .

1)与Tracesource工作,我需要先创建实例。什么是MS考虑在不同类别或组件共享这种情况?我应该创建一个静态singleton属性一个伪类?什么是你在那种情况下做的。

1) To work with Tracesource I need to create instance first. What is MS thinking about sharing this instance across various classes or assemblies? Should I create one dummy class with static singleton property? What are you doing in that case.

2)为什么需要TraceSource实例?每个属性格式是在配置文件中所描述。基于Trace类旧的逻辑并不需要一些实例,并提供仅静态方法的工作方式。

2) Why do I need TraceSource instance? Every propery is described in the configuration file. The old logic based on Trace class did not require some instance and provided the way to work with static methods only.

推荐答案

* 1。只要定义TraceSource在要使用它的每个类。您可以使TraceSource静态的,所以它你没有必要所有类别(类型)需要相同的TraceSource之间共享实例定义它的类的所有实例之间共享。每次decleare新TraceSource(TraceSource TS =新TraceSource(somename);比如,你会得到一个新的TraceSource对象,但是它引用了相同的配置信息,因此,如果您创建的每个几类新TraceSource和你使用相同的名称,每一个,你会得到TraceSource的不同实例,但他们都将进行配置是一样的。总之,没有必要试图类之间共享TraceSource实例。也没有必要创建一个虚拟类的静态单。见下面我的例子,我也包括几个环节,从这里开始,这样描述了如何使用TraceSources工作。

*1. Just define the TraceSource in each class where you want to use it. You can make the TraceSource static so that it shared among all instances of the class you define it in. No need to share the instance among all classes (types) that need the "same" TraceSource. Each time you decleare a new TraceSource (TraceSource ts = new TraceSource("somename"); instance, you get a new TraceSource object, but it references the same config information. So, if you create a new TraceSource in each of several classes and you use the same name for each one, you will get different instances of TraceSource, but they will all be configured the same. In short, there is no need to try to share the TraceSource instances among classes. There is also no need to create a dummy class with a static singleton. See my examples below. I have also included several more links from here on SO that describe how to work with TraceSources.

//
// In this example, tracing in classes A and B is controlled by the "TraceTest" TraceSource
// in the app.config file.  Tracing in class C is controlled by the "TraceTestTwo"
// TraceSource in the app.config.
//
// In addition to using different TraceSource names, you can also use SourceSwitches 
// (in the app.config).  See some examples of app.config in the
// "turning-tracing-off-via-app-config" link below.
//

public class A
{
  private static readonly TraceSource ts = new TraceSource("TraceTest");

  public void DoSomething()
  {
    ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");   
  }
}

public class B
{
  //
  //Use the same config info for TraceTest in this class
  //It's ok to use a different instance of TraceSource, but with the same name,
  //in this class, the new instance will be configured based on the params in the
  //app.config file.
  //
  private static readonly TraceSource ts = new TraceSource("TraceTest");

  public void DoSomething()
  {
    ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");   
  }
}

public class C
{
  //
  //Use a different TraceSource in this class.
  //
  private static readonly TraceSource ts = new TraceSource("TraceTestTwo");

  public void DoSomething()
  {
    ts.TraceEvent(TraceEventType.Warning, 2, "File Test not found");   
  }
}

* 2。一个好处使用多个TraceSources是,你必须更精确地控制您的跟踪。您可以在不同的层面,通过TraceTest在一个级别(或根本没有),并通过TraceTestTwo追踪(或再次,一点都没有)。您可以将每个TraceSource到它自己的TraceListener或发送全部相同的TraceListener,或混搭。比较各个TraceSources的配置调整,只用在Trace类的静态方法的限制的能力。您可以配置里的跟踪的信息去(这TraceListener的(S)),或在跟踪的信息化水平,但一样可以使用TraceSources当你无法控制每班或每个功能区的水平。最后,还有一个好处多TraceSources是,你可以在你的输出获得了自由的上下文信息。默认情况下(或可选,我记不清了),在的TraceListener将日志记录信息的TraceSource的名称。所以,你可以看看你的输出线,得到它的出身,而不必把日志的上下文信息的调用点的类或功能区的一些想法。在上面的code实例,从类A和B的输出轨迹将被标记为TraceTest,并从B级输出的跟踪将被标记为TraceTestTwo。

*2. One benefit to using multiple TraceSources is that you have more granular control over your tracing. You can trace via "TraceTest" at one level (or not at all) and via "TraceTestTwo" at a different level (or, again, not at all). You can send each TraceSource to its own TraceListener or send all to the same TraceListener, or mix and match. Compare the ability to tailor the configuration of individual TraceSources to the limitation of only using the static methods on the Trace class. You can configure where the "trace" information goes (which TraceListener(s)) or the level of the "trace" information, but you cannot control the level per class or per functional area like you can when using TraceSources. Finally, one more benefit to multiple TraceSources is the "free" context information that you can get in your output. By default (or optionally, I can't remember), the TraceListener will log the name of the TraceSource that logged a message. So, you can look at that line in your output and get some idea of the class or functional area where it came from without having to put a log of contextual information in the call site. In the code examples above, the trace output from classes A and B will be tagged with "TraceTest" and the trace output from class B will be tagged with "TraceTestTwo".

请原谅链接轰击之下,但我已经发布了一些pretty的良好的信息(如果我不这样说我自己!)关于TraceSource和System.Diagnostics程序中来。

Please forgive the link bombardment below, but I have posted some pretty good information (if I do say so myself!) about TraceSource and System.Diagnostics in the past.

如果您要使用TraceSource,可以考虑使用本提到的库,以便后期进行格式化你的输出像log4net的/ NLOG:

If you are going to use TraceSource, consider using the library mentioned in this SO post for formatting your output like log4net/NLog:

<一个href="http://stackoverflow.com/questions/208735/does-the-net-tracesource-tracelistener-framework-have-something-similar-to-log4n">Does在净TraceSource / TraceListener的框架有类似的log4net的格式化程序的东西吗?

请参阅我在这篇文章了解更多信息使用TraceSource和你如何能提高你的TraceSource体验的一些想法。

See my answer in this post for more info on using TraceSource and some ideas on how you can improve your "TraceSource experience".

更​​多信息:<一href="http://stackoverflow.com/questions/2479129/add-trace-methods-to-system-diagnostics-tracelistener/3567799#3567799">Add跟踪方法,以System.Diagnostics.TraceListener

更​​多信息:<一href="http://stackoverflow.com/questions/3213502/system-diagnostics-debug-namespace-vs-other-logging-solutions-log4net-ms-enterp/3842180#3842180">System.Diagnostics.Debug命名空间VS其他记录解决方案(log4net的,MS企业库等)

更​​多信息:<一href="http://stackoverflow.com/questions/4144394/turning-tracing-off-via-app-config/4146336#4146336">Turning通过的app.config

这篇关于如何使用TraceSource跨类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆