问题描述
我写了来自System.Diagnostics.TraceListener像这样一个Log类
I wrote a Log class derived from System.Diagnostics.TraceListener like so
public class Log : TraceListener
这作为一个包装到log4net的,并允许人们使用System.Diagnostics程序跟踪,像这样
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)
我希望有此加入到System.Diagnostics.Trace因此它可用于像
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 :(
推荐答案
这是这样晚了,但你有没有考虑过使用TraceSource? TraceSources给你,你可以用它来登录到System.Diagnostics程序(这意味着你可以用一个扩展方法,你提出你的问题扩展它们)实际的对象实例。 TraceSources通常被配置在App.config中(类似于你将如何配置log4net的记录器)。您可以控制记录并跟踪监听器监听的水平。所以,你可以有应用程序code,编程对TraceSource,这可能看起来是这样的:
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:
public class MyClassThatNeedsLogging
{
private static TraceSource ts =
new TraceSource(MethodBase.GetCurrentMethod().DeclaringType.Name);
//Or, to get full name (including namespace)
private static TraceSource ts2 =
new TraceSource(MethodBase.GetCurrentMethod().DeclaringType.FullName);
private count;
public MyClassThatNeedsLogging(int count)
{
this.count = count;
ts.TraceEvent(TraceEventType.Information, 0, "Inside ctor. count = {0}", count);
}
public int DoSomething()
{
if (this.count < 0)
{
ts.TraceEvent(TraceEventType.Verbose, 0, "Inside DoSomething. count < 0");
count = Math.Abs(count);
}
for (int i = 0; i < count; i++)
{
ts.TraceEvent(TraceEventType.Verbose, 0, "looping. i = {0}", i);
}
}
}
您还可以创建使用任何名称(即它不必是类名)TraceSources:
You can also create TraceSources using any name (i.e. it doesn't have to be the class name):
TraceSource ts1 = new TraceSource("InputProcessing");
TraceSource ts2 = new TraceSource("Calculations");
TraceSource ts3 = new TraceSource("OutputProcessing");
正如我前面提到的,每个TraceSource通常配置在app.config文件,以及日志记录级别和听者应接收的输出。
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:
public static class TraceSourceExtensions
{
public static void TraceVerbose(this TraceSource ts, string message)
{
ts.TraceEvent(TraceEventType.Verbose, 0, message);
}
}
如果你需要做TraceSource了更多的自定义(如添加额外的水平),这是一个pretty的很好的文章,介绍了如何做到这一点:
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
如果您正在使用最终log4net的你的TraceListener内(并且用它来定义一个名为记录器,日志记录级别等),你可能不需要配置许多TraceSources。你甚至可以只配置一个(他的名字将被熟知的),或者你也许可以创建一个编程方式,将其设置为登录所有,并把它挂到您的具体的TraceListener。
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.
在最后,而不是通过静态的跟踪对象的记录,您可以登录通过TraceSource实例。如果配置了一个TraceSource,它的名字是众所周知的,有效的TraceSource可以创建(记录)的任何地方像这样的:
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:
TraceSource ts = new TraceSource("logger");
ts.TraceEvent(TraceEventType.Information, 0, "Hello World!");
//Or, via your extension method
ts.TraceVerbose(TraceEventType.Verbose, 0, "Logged via extension method");
可能有更好的方法来完成你正在试图完成的任务,但是这可能会给你一些思考使用TraceSource VS静态Trace类有关。
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.
这篇关于添加跟踪方法System.Diagnostics.TraceListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!