简体   繁体   English

如何在.net中实现跟踪

[英]How to implement tracing in .net

I am implementing a tracing mechanism myself. 我正在自己实施跟踪机制。 Now what I'd like to know is what is the best way to implement this mechanism(I mean, the architecture). 现在,我想知道的是实现这种机制的最佳方法是什么(我的意思是架构)。 First I thought of creating a class like this: 首先,我想创建一个这样的类:

class Tracer {
    public void NewEventRegistered(etc) {...} //this is the method I use to send this class the events I want to register

    event newEventRegistered(..) //this is the event the user is going to use to catch all the events in the application that have been sent to NewEventRegistered method.
}

But then I saw there is an TraceListener class that let's me implement Write and WriteLine methods. 但是然后我看到有一个TraceListener类,让我实现Write和WriteLine方法。 The big drawback is that both those methods allow only strings as arguments, and I'd want to be able to send a lot more info than strings. 最大的缺点是这两种方法都只允许将字符串作为参数,并且我希望能够发送比字符串更多的信息。 What should I do? 我该怎么办? Maybe my first ideia is the best? 也许我的第一个想法是最好的?

edit: I do know there are a lot of tracing mechanisms like PostSharp and so, I'm just doing this for learning purposes! 编辑:我确实知道有很多跟踪机制,例如PostSharp,所以,我只是出于学习目的而这样做!

The Write and WriteLine functions that accept strings only represent the minimum pair of functions you have to override. 接受字符串的Write和WriteLine函数仅表示您必须覆盖的最小一对函数。 There are other write functions in TraceListener you can optionally override that accept objects. TraceListener中还有其他写函数,您可以选择覆盖接受对象。

TraceListener is, however, designed for use with the framework tracing functions. 但是,TraceListener设计为与框架跟踪功能一起使用。 So if you're not going to use the class to "listen" to trace output going through the framework, you may be better off with an interface more suited to your particular tracing needs. 因此,如果您不打算使用该类来“监听”以跟踪通过框架的输出,则最好使用更适合您的特定跟踪需求的接口。

Perhaps inheriting from System.IO.TextWriter would be better for your case. 从System.IO.TextWriter继承可能对您的情况更好。 It has many write methods, but I believe they route all their output through Write and WriteLine, so if you just override those two functions, you get a lot for free. 它有许多写方法,但我相信它们会通过Write和WriteLine路由所有输出,因此,如果您只是覆盖这两个函数,则可以免费获得很多。

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

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