Is Debug.WriteLine () safe?

Is Debug.WriteLine () thread safe?

According to this , it is thread safe. But in my multi-threaded program, I get some strange conclusion.

For instance:

The code

// these statements are found throughout the program Debug.WriteLine("Polled database. {0} batch items retrieved.", items.Count()); Debug.WriteLine("Queued batch item: {0}", bm.BatchName); Debug.WriteLine("Discarded batch item: {0} already queued.", bm.BatchName); Debug.WriteLine("Creating task for batch item: {0}", bm.BatchName); Debug.WriteLine("Removed batch item from processing items collection: {0}", bm.BatchName); Debug.WriteLine("Could not remove batch item from processing items collection: {0}", bm.BatchName); Debug.WriteLine("Begin Processing: {0}", bm.BatchName); Debug.WriteLine("End Processing: {0}", bm.BatchName); 

Exit

 "Polled database. 0 batch items retrieved." "Polled database. 0 batch items retrieved." "Polled database. 0 batch items retrieved." "Polled database. 0 batch items retrieved." "Polled database. 1 batch items retrieved." "ronnie batch: Queued batch item: {0}" "ronnie batch: Creating task for batch item: {0}" "Begin Processing: ronnie batch" "Polled database. 1 batch items retrieved." "ronnie batch: Discarded batch item: {0} already queued." "End Processing: ronnie batch" "ronnie batch: Removed batch item from processing items collection: {0}" "Polled database. 0 batch items retrieved." 

You can see that everything starts with rails using ronnie batch: Queued batch item: {0} If I use string.Format () first, I have no problem. What's happening?

+6
source share
2 answers

The problem is that you are not calling the overload you are thinking about. You call Debug.WriteLine(string, string) , which uses the first parameter as a message and the second as a category, not a format argument.

The easiest way to fix this is to pass the object argument to force it to use Debug.WriteLine(string, params object[]) overload:

 Debug.WriteLine("Queued batch item: {0}", (object) bm.BatchName); 

A slightly longer approach, but one that is perhaps more object-oriented, is to explicitly create an array:

 Debug.WriteLine("Queued batch item: {0}", new object[] { bm.BatchName }); 

Or (just to save delivery parameters :) call string.Format explicitly to cause Debug.WriteLine(string) to overload:

 Debug.WriteLine(string.Format("Queued batch item: {0}", bm.BatchName)); 

or when you simply include the argument directly at the end:

 Debug.WriteLine("Queued batch item: " + bm.BatchName); 

Alternatively, you can create your own convenience method that does not have an additional, useless (in your case) overload.

+11
source

I know this is an old thread and it is not very relevant for the question, but with newer versions of .NET you can also use interpolation:

 Debug.WriteLine($"Queued batch item: {bm.BatchName}"); 

Also, if you find yourself here, yes, it is thread safe. This is not what I found out the hard way:

 Console.WriteLine($"Queued batch item: {bm.BatchName}"); 
0
source

Source: https://habr.com/ru/post/894397/


All Articles