1

这一切的重点是运行一个单独的线程(可以是类似的东西:Task、Action)并记录“开始”和“结束”,最好尽量减少代码影响(更改)并且记录对代码透明,只是使用不同的类

最近我决定从类继承,System.Threading.Thread作为实现诸如“线程 x 启动”之类的日志记录的一种方式。和“线程 x 结束。” 到一个文件,用于调试。由于该类是密封的,因此我使用组合将 Thread 添加为成员并调用相应的函数、访问器和所有必要的。我对如何做有一个简单的想法,所以我想像

namespace MyNamespace
{
    class Thread
    {
        private System.Threading.Thread thread;

        //wrap constructor properties methods...
        //...

        public void Start()
        {
                log(thread.name + " start.");
                thread.Start();
                log(thread.name + " end.");
        }

        //...
    }
}

现在,在包装现有功能(并使用新的命名空间而不是 System.Threading)的所有这些麻烦之后......结果证明这很糟糕,因为如果我没记错的话,这个调用来自调用线程,其次是“结束”不记录。日志记录主要用于调试并具有所发生事件的非易失性状态。

编辑:

这是使用 Ahmed 答案的简化代码,这很好,但仍然存在的问题是,如果SimulatedThread抛出异常,则“Ending ...”不会记录“MainThread”或“CustomThread”。如果CustomThread 异常终止,则应记录“MainThread”和“CustomThread”的“Ending...”。(异常处理不相关)。“MainThread”应该是“CustomThread”的防弹。

全面实施

using System;
using System.Security;
using System.Threading;

namespace CS_Tests_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Logger.Log("Starting...");
            try
            {
                Thread.CurrentThread.Name = "MainThread";
                CustomThread thread = new CustomThread(SimulateThread)
                    {
                        Name = "CustomThread",
                    };
                thread.Start();
                thread.Join();
            }
            catch (Exception ex)
            {
                Logger.Log("Cought exception:" + ex.ToString());
            }
            Logger.Log("Ending...");
        }

        private static void SimulateThread()
        {
            Logger.Log("Running...");
            Thread.Sleep(2000);
            throw new Exception("Test Exception");
        }
    }

    public static class Logger
    {
        public static void Log(String message)
        {
            if (Thread.CurrentThread.Name == null)
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " Thread.CurrentThread.Name is NULL -> " + message);
            }
            else
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " " + Thread.CurrentThread.Name + " -> " + message);
            }
        }
    }

    public class CustomThread
    {
        public Thread threadInstance { get; private set; }
        public CustomThread(ThreadStart threadStart)
        {
            threadInstance = new Thread(() =>
            {
                Logger.Log("Starting...");
                threadStart();
                Logger.Log("Ending...");
            });
        }

        public string Name { get { return threadInstance.Name; } set { threadInstance.Name = value; } }

        [SecuritySafeCritical]
        public void Join()
        {
            threadInstance.Join();
        }

        public void Start()
        {
            threadInstance.Start();
        }
    }
}
4

1 回答 1

0

ThreadStart委托中封装日志记录:

public class Logger{
    public void Log(String message){
        Console.WriteLine(message);
    }
}

public class CustomThread {
    public Thread ThreadInst { get; private set; }
    public Logger logger = new Logger();
    public CustomThread(ThreadStart threadStart) {
        ThreadInst = new Thread(() =>
        {
            logger.Log(Thread.CurrentThread.Name + " Starting...");
            threadStart();
            logger.Log(Thread.CurrentThread.Name + " Ending...");
        });
    }
    public void Start() { ThreadInst.Start(); }
}
class Program
{

    static void Main(string[] args)
    {
        new CustomThread(() => Thread.Sleep(2000)).Start();
    }
}

编辑:添加了捕获和记录异常:

    public CustomThread(ThreadStart threadStart) {
        ThreadInst = new Thread(() =>
        {
            logger.Log(Thread.CurrentThread.Name + " Starting...");
            try {
                threadStart();
            } catch (Exception ex) {
                logger.Log("Error in thread" + Thread.CurrentThread.Name + " " + ex.Message);
            }
            logger.Log(Thread.CurrentThread.Name + " Ending...");
        });
    }
于 2013-06-27T08:08:06.500 回答