Claire wrote:
Ive a static logging routine that's called by all my objects in the
application.
Ive several threads running in my application and Id like the log file to
show the thread ID so I can trace a single thread if I need to.
Ive had a look through system.threading.thread.currentthread
methods/properties but nothing immediately strikes me, how can I obtain a
value for the ID or similar?
thanks
Claire
i'v done this like that (plus an identation...)
public static void write(string textToAdd, int pm)
{
int indent;
if (!threads.ContainsKey(Thread.CurrentThread))
{
threads.Add(Thread.CurrentThread, 0);
indent = 0;
}
else
{
indent = (int)threads[Thread.CurrentThread];
}
// séparation des threads
if (lastThread != Thread.CurrentThread)
swFromFile.WriteLine("---------------------------------------------------------------------------------------
"+Thread.CurrentThread.Name);
// date
swFromFile.Write(DateTime.Now.ToLongTimeString()+" ");
// indentation
if (pm == -1)
indent--;
for (int i=0; i<indent; i++)
{
swFromFile.Write("\t");
}
if (pm == -1)
indent++;
threads[Thread.CurrentThread] = indent+pm;
// texte
swFromFile.WriteLine(textToAdd);
swFromFile.Flush();
lastThread = Thread.CurrentThread;
}