I am trying to disentangle my logs by grouping my log messages together. Ideally, I'd append to each message some identifier indicating that it is part of a group of commands achieving some piece of work.
If this were a single threaded application, the thread's ID would be a candidate. However, this solution makes heavy use of async Task
s, which precludes its use. Is it possible to get something like the following to work, where every execution of Main()
would always print out the same thread ID?
static async void Main()
{
var t = Task.Run(async () => await MyAsyncMethod());
await t;
}
private static async Task MyAsyncMethod()
{
Debug.WriteLine("Log message: {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(1000);
Debug.WriteLine("Log message: {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(1000);
Debug.WriteLine("Log message: {0}", Thread.CurrentThread.ManagedThreadId);
}