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.)
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With