【问题标题】:Can tracing via TraceSource be configured through code from external assembly是否可以通过外部程序集的代码配置通过 TraceSource 的跟踪
【发布时间】:2015-03-20 05:47:14
【问题描述】:

大多数TraceSource 跟踪示例显示了它是如何通过配置完成的。我正在尝试通过代码来实现这一点。

我有一个简单的组装如下:

using System;
using System.Diagnostics;
using System.Threading;

namespace TracingLib
{
    public class AppTracer
    {
        public event EventHandler AppStarted;
        public event EventHandler AppStopped;
        private bool CancelRequested = false;
        public void Start()
        {
            Thread thread = new Thread(new ThreadStart(() =>
            {
                if(AppStarted != null) AppStarted(this, new EventArgs());

                TraceSource ts = new TraceSource("ThreadSource");

                ts.TraceInformation("Thread Begins");
                //ts.Flush();
                int i = 0;
                while (!CancelRequested)
                {

                    ts.TraceInformation("i: {0}", i++);
                    //ts.Flush();
                    Debug.Print("i : {0}", i);
                    Thread.Sleep(5000);
                }

                if (AppStopped != null) AppStopped(this, new EventArgs());
            }));
            thread.Start();
        }

        public void Stop()
        {
            CancelRequested = true;
        }
    }
}

我正在控制台应用程序中使用它。

using System;
using System.Threading;
using TracingLib;

namespace caTracingLibImplementation
{
    class Program
    {
        static void Main(string[] args)
        {
            AppTracer tracer = new AppTracer();
            ManualResetEvent waiter = new ManualResetEvent(false);
            tracer.AppStopped += (sender, e) =>
            {
                waiter.Set();
            };

            TraceSource ts = new TraceSource("ThreadSource");            
            ts.Listeners.Add(new ConsoleTraceListener());
            var sw = new SourceSwitch("foo");
            sw.Level = SourceLevels.Warning;
            ts.Switch = sw;
            tracer.Start();
            Console.WriteLine("AppTracer started...");
            Thread.Sleep(10000);
            tracer.Stop();
            Console.WriteLine("AppTracer stopped...");
            Console.WriteLine("Waiting to stop...");
            waiter.WaitOne();
            Console.WriteLine("End of program");
        }
    }
}

如果我尝试通过控制台应用程序启用跟踪,我将看不到跟踪消息。

【问题讨论】:

  • 一旦您获得对内部跟踪源列表的引用(每个 AppDomain 1 个),您可以对其执行任何操作(通常在启动时通过配置完成),此代码说明了该技术,我'看看我是否可以创建一个 woking 示例:stackoverflow.com/questions/23664573/…

标签: .net trace tracesource


【解决方案1】:

首先,您需要向 AppTracer 中定义的 TraceSource 提供 TraceListener。

此代码可以使用配置文件工作,因为 .NET 框架保留从配置文件获取的每个侦听器的单个实例,并根据初始字符串参数将它们分配给每个 TraceSource。

如果在 app.config 中未定义 TraceSource 的字符串参数,则不会有任何监听器保存在全局状态中。因此 TraceSource 的每个实例都必须提供给它的所有侦听器。

TraceSource.Initialize() Source

其次,开关:sw.Level = SourceLevels.Warning; 将禁用使用以下命令记录的所有语句:ts.TraceInformation()

【讨论】:

  • 究竟“什么”正在读取配置文件以通过跟踪源启用跟踪?
  • 您最好通读链接源以获得最完整的解释。 sourceElement.Listeners 是魔法发生的地方。
  • 我想我剩下的唯一选择是从 AppTracer 公开 TraceSource 对象并从正在执行的程序集中向它添加侦听器...
【解决方案2】:

如果您在运行时配置 TraceSource,那么我认为最适合您的代码的解决方案是:

  1. 将该动态 TraceSource 注入 AppTracer 的构造函数中。
  2. 或通过 Start() 传入 TraceSource。

我会倾向于 #1 而不是 #2。

正如 user1112560 所述,如果您的开关级别设置为警告,您将看不到 TraceInformation 输出。优先级为关闭、错误、警告、信息、详细。见MSDN Trace Switches

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多