添加/删除 TraceListener 到所有 TraceSources [英] add/remove TraceListener to all TraceSources

查看:42
本文介绍了添加/删除 TraceListener 到所有 TraceSources的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种为所有现有 TraceSource 添加和删除 TraceListener 的方法.

I am looking for a way to add and remove a TraceListener for all existing TraceSources.

(我不确定我的方法在这里是否正确,我还可以使用哪些其他方法?基本上我想将所有跟踪输出记录到一个使用当前项目名称作为文件名的文件中.每当用户创建或重新打开项目时,我都想将日志附加到正确的文件中.一次只能打开一个项目.)

(I am not sure my approach is correct here, what other ways could I use? Basically I want to log all trace output into a file that uses the current project name as filename. Whenever a user creates or reopens a project, I want to append logs to the correct file. There can only be one project open at a time.)

代码示例:

我在应用程序中创建了多个 TraceSource,每个类一个

I create several TraceSources in my application, one for each class

public class Class1
{
    private static readonly System.Diagnostics.TraceSource trace = 
            new System.Diagnostics.TraceSource("Class1");
}

public class Class2
{
    private static readonly System.Diagnostics.TraceSource trace = 
            new System.Diagnostics.TraceSource("Class2");
}

我现在想在运行时向我的所有 traceSource 添加或删除一个 traceListener,如下所示:

I now want to add or remove a traceListener to all my traceSources at Runtime, like this:

private System.Diagnostics.TextWriterTraceListener myListener;

private onProjectOpen()
{
    // user created a new project or opened an existing one
    myListener = new System.Diagnostics.TextWriterTraceListener("log-"+projectname+".log");
    ALL_TRACESOURCES.Add ( myListener) ; // <-- how to do this?
}

private onProjectClose()
{
    // user closed a project
    ALL_TRACESOURCES.Remove( myListener) ; // <-- how to do this?
    myListener.Flush();
    myListener.Close();
    myListener.Dispose(); // <-- not sure if all this is neccessary
}

到目前为止,如果不公开我的所有 traceSources(似乎是个坏主意),然后像这样列出我的所有类,我就没有办法做到这一点:

So far I found no way to do this without making all my traceSources public (seems like a bad idea) and then listing all my classes like this:

Class1.Trace.Add( myListener );
Class2.Trace.Add( myListener );
...

这在几个层面上似乎都是一个糟糕的设计选择.

which seems like a bad design choice on several levels.

将我所有的 TraceSources 添加到每个类的构造函数中的自定义全局集合中(容易忘记/搞砸;全局变量不好)

Add all my TraceSources to a custom global collection in the constructor of each class (Easy to forget / mess up; and global variables are bad )

有没有更好的办法?基本上我正在寻找一种设置另一个默认侦听器的方法

Is there a better way? Basically I am looking for a way to set another default listener

推荐答案

基于 这个 StackOverflow 答案这个回答

这是一种方法:

    private static void AttachToAllTraceSources(TraceListener yourListener)
    {
        TraceSource ts = new TraceSource("foo");
        List<WeakReference> list = (List<WeakReference>)GetInstanceField(typeof(TraceSource), ts, "tracesources");
        foreach(var weakReference in list)
        {
            if(weakReference.IsAlive)
            {
                TraceSource source = (weakReference.Target as TraceSource);
                if(source != null && source.Name != "foo")
                {
                    source.Listeners.Add(yourListener);
                }
            }
        }
    }

这篇关于添加/删除 TraceListener 到所有 TraceSources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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