How can I give an ID to CancellationToken?

64 Views Asked by At

I have a system that links tokens received from different places. Then, it uses this merged token to perform some task. The final merged token can consist of 5 different tokens.

While the task is running, I can call cancellationTokenSource.Cancel() in any of the places where the CancellationTokenSource came from for the merge. Then the exception is caught using try-catch, and a message is printed to the console.

catch (Exception ex) {
    if (ex is TaskCanceledException taskCanceledException) {
        var token = taskCanceledException.CancellationToken;
        Console.WriteLine("Task was canceled via token. " 
            + "Where and when it was created? Idk.");
    }
}

I came across a bug where an old canceled token is still in use, and I have no idea where and when it was created. I could enter this information into the name of the token, which I would then take out from the exception and print in the console.

However, the token does not have such a field.

How do people using CancellationToken-s deal with debugging these situations?

0

There are 0 best solutions below