3

我有一个应用程序。在调试过程中,我将重要信息记录到文件或交互界面中。

这是示例: 演示

您会看到第一行日志信息没有线程名称。我想添加一个名字。但是哪里?

    public static void Start()
    {
        lock (SyncVar)
        {
            if (State == State.Stopped)
            {
                s_State = State.Starting;
                ThreadStart ts = new ThreadStart(MainCode);
                s_MainCodeThread = new Thread(ts);
                s_MainCodeThread.Name = "IvrApplication";
                s_MainCodeThread.Start();
                Log.Write("IvrApplication Starting...");
            }

和....

    public static void MainCode()
    {

        try
        {
            s_WorkingFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            Log.Write("IvrApplication::MainCode() Starting...");

            // Start Other Threads...
            try
            {

至于日志,我们有

    private static Log s_Log;
    public static Log Log
    {
        get { return s_Log; }
    }

实际上日志来自一个dll

这是部分元数据。

namespace VoiceElements.Common
{
   public class Log
   {
       [ThreadStatic]
       public static string Identifier1;
       [ThreadStatic]
       public static string Identifier2;
       public int LogLevel;

       public Log(string logname);

       public bool AlwaysExpanded { get; set; }

       public event MessageLogged MessageLogged;

       public void CloseLog();
       public void Write(string LogEntry);
4

3 回答 3

4

您只需要命名当前线程:

Thread.CurrentThread.Name = "myThread";
于 2013-11-13T15:25:49.430 回答
3

第一条日志消息由应用程序的默认线程输出。您应该能够设置:

Thread.CurrentThread.Name = "SomeName";

确保在 之前拨打该电话Log.Write("IvrApplication Starting...");

于 2013-11-13T15:26:01.177 回答
0

您应该重命名执行 MainCode 方法的线程:

public static void MainCode()
{
    //SET A NAME HERE
    Thread.CurrentThread.Name = "Main thread"

    try
    {
        s_WorkingFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

        Log.Write("IvrApplication::MainCode() Starting...");

        // Start Other Threads...
        try
        {
于 2013-11-13T15:28:53.640 回答