Debug.WriteLine() 与 Console.WriteLine() 处理文化的方式不同。为什么?

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

考虑以下控制台应用程序代码:

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

如评论中所述,Console.WriteLine() 打印 19/01/2014Debug.WriteLine() 打印 01/19/2014

更糟 - Debug.WriteLine("{0}", date) 给出与 Debug.WriteLine(date) 不同的输出...

是否期望 Debug.WriteLine() 忽略线程的文化设置?

有没有办法让 Debug.WriteLine() 使用线程的文化设置?或者我必须使用 String.Format() 并将结果传递给 Debug.WriteLine()?

(注意:我在 Windows 8.1 64 位 en-GB 上 运行 使用 Visual Studio 2013 和 .Net 4.51 以及调试 AnyCPU 构建。)

这是明确处理的in the source

也有道理。
调试输出不应受到最终用户文化的影响;无论代码在何处,您都希望调试日志保持一致 运行.

您正在使用的重载 explicitly ignores the culture 通过使用 InvariantCulture:

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

所有其他重载都不做任何与文化相关的事情。您可以 "workaround" 通过使用需要 string:

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

通过这样做:

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);

现在都打印:

19/01/2014 00:00:00 
19/01/2014 00:00:00