TraceSource和TraceListener悄无声息地无能为力 [英] TraceSource and TraceListener quietly fail to do anything

查看:82
本文介绍了TraceSource和TraceListener悄无声息地无能为力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当System.Diagnostics安静地根本无法执行任何操作时,如何对它进行故障排除?

How do I troubleshoot System.Diagnostics trace when it quietly fails to do anything at all?

推荐答案

您问到我很高兴!这个问题最近发生在我身上.我怀疑TraceSwitch es,TraceSource s和TraceListener s链中的某些东西出了问题.但是,由于没有任何痕迹,也没有错误消息,因此我需要更多信息. BCL的作者有帮助地将所有诊断信息放在一个私有列表中,一旦垃圾收集器感觉到它的值,它们的值就会消失.尽管如此,该信息仍然足够好,可以通过反射进行访问.原来,我有几个TraceSource,但是有问题的那个没有将Switch设置为Off以外的其他设置. (另一个不好的迹象是没有监听器或只有默认"监听器)

I'm so glad you asked! This problem happened to me recently. I suspected something in the chain of TraceSwitches, TraceSources and TraceListeners had gone awry. But with no trace and no error messages, I needed more information. The authors of the BCL helpfully put all the diagnostic information in a private list whose values disappear when ever the garbage collector feels like it. Still, the info was good enough and accessible via reflection. It turned out that I had several TraceSource's but the one in question didn't have a Switch set to something other than Off. (Another bad sign would be no listeners or only the "Default" listener)

这是带有标签DumpText的页面的Page_Load事件.我确定此代码可以适用于Console或WinForms.

This is the Page_Load event for a page with a label called DumpText. I'm sure this code could be adapted for Console or WinForms.

protected void Page_Load(object sender, EventArgs e)
{
    TraceSource ts = new TraceSource("foo");
    List<WeakReference> list = (List<WeakReference>)GetInstanceField(typeof(TraceSource), ts, "tracesources");
    Dictionary<string, TraceSource> sources = new Dictionary<string, TraceSource>();
    foreach (var weakReference in list)
    {
        if (!weakReference.IsAlive) continue;
        TraceSource source = (weakReference.Target as TraceSource);
        if (source == null || source.Name == "foo") continue;
        if (sources.ContainsKey(source.Name)) continue;
        sources.Add(source.Name, source);
    }
    StringBuilder sb = new StringBuilder();

    foreach (KeyValuePair<string,TraceSource> kvp in sources.OrderBy((x) => x.Key))
    {
        TraceSource source = kvp.Value;
        if (source == null)
        {
            continue;
        }
        sb.Append("<h3>");
        sb.Append(source.Name);
        sb.Append(" - ");
        sb.Append(source.Switch.Level);
        sb.Append("</h3>");

        if (source.Switch.Level == SourceLevels.Off)
        {
            continue;
        }
        foreach (TraceListener l in source.Listeners)
        {
            sb.Append(l.Name);
            sb.Append(" - ");
            sb.Append(l.GetType().ToString());
            sb.Append("<br/>");

            foreach (string att in l.Attributes.Values)
            {
                sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                sb.Append(att);
                sb.Append(",");
            }
        }
        sb.Append("<br/>");
    }
    this.DumpText.Text = sb.ToString();
}

internal static object GetInstanceField(Type type, object instance, string fieldName)
    {
        BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
            | BindingFlags.Static;
        FieldInfo field = type.GetField(fieldName, bindFlags);
        return field.GetValue(instance);
    }

这篇关于TraceSource和TraceListener悄无声息地无能为力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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