Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug.WriteLine() versus Console.WriteLine() handles culture differently. Why?

Consider the following Console App code:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

DateTime date = new DateTime(2014, 01, 19);

Console.WriteLine("{0}", date); // Prints 19/01/2014
Debug.WriteLine("{0}", date);   // Prints 01/19/2014
Debug.WriteLine(date);          // Prints 19/01/2014

As noted in the comments, the Console.WriteLine() prints 19/01/2014 while the Debug.WriteLine() prints 01/19/2014.

Even worse - Debug.WriteLine("{0}", date) gives different output from Debug.WriteLine(date)...

Is it expected that Debug.WriteLine() ignores the thread's culture settings?

Is there a way to make Debug.WriteLine() use the thread's culture settings? Or must I use String.Format() and pass the result to Debug.WriteLine()?

(Note: I'm running this on Windows 8.1 64-bit, en-GB, using Visual Studio 2013 with .Net 4.51 with a debug AnyCPU build.)

like image 345
Matthew Watson Avatar asked Jan 19 '15 15:01

Matthew Watson


People also ask

What is the difference between console WriteLine and debug WriteLine?

Console. WriteLine writes to the standard output stream, either in debug or release. Debug. WriteLine writes to the trace listeners in the Listeners collection, but only when running in debug.

Does console WriteLine affect performance?

Yes, executing Console. WriteLine takes a measureable amount of time. Removing the Console. WriteLine call or changing it to a buffered background thread writing the data would really speed up the application.

What is console WriteLine () good for?

Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.

What is debug WriteLine?

WriteLine(String) Writes a message followed by a line terminator to the trace listeners in the Listeners collection. WriteLine(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.


2 Answers

This is explicitly handled in the source.

It makes sense, too.
Debug output should not be affected by the end-users culture; you want your debug logs to be consistent no matter where the code is running.

like image 198
SLaks Avatar answered Sep 21 '22 14:09

SLaks


The overload you are using explicitly ignores the culture by using InvariantCulture:

public static void WriteLine(string format, params object[] args) 
{
    TraceInternal.WriteLine(String.Format(CultureInfo.InvariantCulture, format, args));
}

All the other overloads don't do anything related to culture. You can "workaround" this by using the overload which takes a string:

public static void WriteLine(string message, string category)
{
    TraceInternal.WriteLine(message, category);
}

By doing this:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

DateTime date = new DateTime(2014, 01, 19);

var formatedDate = string.Format("{0}", date);
Console.WriteLine(formatedDate);
Debug.WriteLine(formatedDate);

Now both print:

19/01/2014 00:00:00 
19/01/2014 00:00:00
like image 40
Yuval Itzchakov Avatar answered Sep 18 '22 14:09

Yuval Itzchakov