将 Trace 方法添加到 System.Diagnostics.TraceListener
如下所示。
public class Log : TraceListener
它充当 Log4Net 的包装器,并允许人们像这样使用 System.Diagnostics 跟踪。
Trace.Listeners.Clear();
Trace.Listeners.Add(new Log("MyProgram"));
Trace.TraceInformation("Program Starting");
我编写了一个从 System.Diagnostics.TraceListener 派生的 Log 类, ,信息)
我想将其添加到 System.Diagnostics.Trace 中,以便可以使用它,例如
Trace.TraceVerbose("blah blah");
Trace.TraceAlert("Alert!");
有什么方法可以使用扩展类来做到这一点吗?我尝试过
public static class TraceListenerExtensions
{
public static void TraceVerbose(this Trace trace) {}
}
,但传入的跟踪实例上没有任何内容暴露:(
I wrote a Log class derived from System.Diagnostics.TraceListener like so
public class Log : TraceListener
This acts as a wrapper to Log4Net and allows people to use System.Diagnostics Tracing like so
Trace.Listeners.Clear();
Trace.Listeners.Add(new Log("MyProgram"));
Trace.TraceInformation("Program Starting");
There is a request to add additional tracing levels then the default Trace ones (Error,Warning,Information)
I want to have this added to the System.Diagnostics.Trace so it can be used like
Trace.TraceVerbose("blah blah");
Trace.TraceAlert("Alert!");
Is there any way I can do this with an extension class? I tried
public static class TraceListenerExtensions
{
public static void TraceVerbose(this Trace trace) {}
}
but nothing is being exposed on the trace instance being passed in :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然已经晚了,但是您考虑过使用 TraceSource 吗? TraceSources 为您提供可用于记录到 System.Diagnostics 的实际对象实例(这意味着您可以按照您在问题中建议的方式使用扩展方法来扩展它们)。 TraceSources 通常在 app.config 中配置(类似于配置 log4net 记录器的方式)。您可以控制日志记录级别以及正在侦听的跟踪侦听器。因此,您可以拥有针对 TraceSource 进行编程的应用程序代码,其可能如下所示:
您还可以使用任何名称创建 TraceSource(即不必是类名):
正如我之前提到的,每个 TraceSource 通常都是经过配置的在 app.config 文件中,以及日志记录“级别”和应该接收输出的侦听器。
对于您的扩展方法,您可以执行以下操作:
如果您需要对 TraceSource 进行更多自定义(例如添加其他级别),这是一篇非常好的文章,描述了如何执行此操作:
http://msdn.microsoft.com/en-us/magazine/cc300790.aspx
如果您最终使用log4net 在 TraceListener 内部(并使用它来定义命名记录器、日志记录级别等),您可能不需要配置许多 TraceSource。您甚至可能只能配置一个(其名称众所周知),或者您可以以编程方式创建一个,将其设置为记录“全部”,并将其连接到您的特定 TraceListener。
最后,您可以通过 TraceSource 实例进行日志记录,而不是通过静态 Trace 对象进行日志记录。如果配置了一个 TraceSource 并且其名称众所周知,则可以在任何地方创建有效的 TraceSource(用于日志记录),如下所示:
可能有更好的方法来完成您想要完成的任务,但这可能会给您一些思考的机会关于使用 TraceSource 与静态 Trace 类。
This is way late, but have you considered using TraceSource? TraceSources give you actual object instances that you can use to log to System.Diagnostics (which means that you could extend them with an extension method as you propose in your question). TraceSources are typically configured in app.config (similar to how you would configure log4net loggers). You can control the level of logging and which trace listeners are listening. So, you could have application code, programmed against TraceSource, that might look something this:
You can also create TraceSources using any name (i.e. it doesn't have to be the class name):
As I mentioned earlier, each TraceSource is typically configured in the app.config file, along with the logging "level" and the listener that should receive the output.
For your extension method, you could do something like this:
If you need to do more customization of TraceSource (like add additional levels), this is a pretty good article that describes how to do that:
http://msdn.microsoft.com/en-us/magazine/cc300790.aspx
If you are ultimately using log4net inside of your TraceListener (and using it to define named loggers, logging levels, etc), you might not need to configure many TraceSources. You might even be able to configure only one (whose name would be well-known) or you might be able to create one programmatically, set it to log "all", and hook it up to your specific TraceListener.
In the end, instead of logging through the static Trace object, you can log through TraceSource instances. If there is one TraceSource configured and its name is well known, a valid TraceSource can be created (for logging) anywhere like this:
There may be better ways to accomplish what you are trying to accomplish, but this might give you something to think about regarding using TraceSource vs the static Trace class.
您的问题是您无法添加扩展方法来作用于静态类。扩展方法的第一个参数是它应该操作的实例。由于
Trace
类是静态的,因此没有这样的实例。您最好创建自己的静态日志包装器,公开您想要的方法,例如TraceVerbose
:这还会将您的日志记录代码与正在使用的实际日志记录框架分离,以便您可以如果您愿意,稍后可以从跟踪日志记录切换到使用 log4net 或其他东西。
Your problem is that you cannot add extension methods to act on static classes. The first parameter of an extension method is the instance that it should operate on. Since the
Trace
class is static, there is no such instance. You are probably better off creating your own static log wrapper, exposing the methods you want to have, such asTraceVerbose
:This will also decouple you logging code from the actual logging framework in use, so that you can later switch from trace logging to using log4net or something else, should you wish to.