C# 如何在.net中实现跟踪

C# 如何在.net中实现跟踪,c#,.net,vb.net,trace,C#,.net,Vb.net,Trace,我自己也在实施追踪机制。现在我想知道的是,实现这种机制(我指的是体系结构)的最佳方式是什么。首先,我想到创建这样一个类: 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 even

我自己也在实施追踪机制。现在我想知道的是,实现这种机制(我指的是体系结构)的最佳方式是什么。首先,我想到创建这样一个类:

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.
}
但后来我看到了一个TraceListener类,让我实现Write和WriteLine方法。最大的缺点是这两种方法都只允许字符串作为参数,我希望能够发送比字符串多得多的信息。我该怎么办?也许我的第一个想法是最好的


编辑:我知道有很多跟踪机制,比如PostSharp,所以我只是为了学习而做的

接受字符串的Write和WriteLine函数只表示必须重写的最小函数对。TraceListener中还有其他写入函数,您可以选择重写这些函数来接受对象

然而,TraceListener设计用于框架跟踪功能。因此,如果您不打算使用该类“侦听”整个框架中的跟踪输出,那么最好使用更适合您特定跟踪需求的接口

也许继承System.IO.TextWriter更适合您的情况。它有许多写入方法,但我相信它们通过write和WriteLine路由所有输出,所以如果您只重写这两个函数,您可以免费获得很多