user1108948
user1108948

Reputation:

Add a thread name

I have an application. During the debugging, I log the important information to file or interactive interface.

Here is the example: demo

You see the first line log information doesn't have a thread name. I want to add a name. But where?

    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...");
            }

And....

    public static void MainCode()
    {

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

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

            // Start Other Threads...
            try
            {

As for Log, we have

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

Actually Log is from a dll

Here is the partial metadata.

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);

Upvotes: 3

Views: 113

Answers (3)

Reda
Reda

Reputation: 2289

You should rename the thread that executes your MainCode method:

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
        {

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69260

That first log message is output by the application's default thread. You should be able to set that:

Thread.CurrentThread.Name = "SomeName";

Make sure to make that call before Log.Write("IvrApplication Starting...");.

Upvotes: 3

pattermeister
pattermeister

Reputation: 3232

You just need to name the current thread:

Thread.CurrentThread.Name = "myThread";

Upvotes: 4

Related Questions