考虑以下控制台应用程序代码:
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/2014
而 Debug.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 上运行此程序,使用带有 .Net 4.51 的 Visual Studio 2013 和调试 AnyCPU 构建。)
最佳答案
这是明确处理的in the source .
这也是有道理的。
调试输出不应受到最终用户文化的影响;无论代码在何处运行,您都希望调试日志保持一致。
关于c# - Debug.WriteLine() 与 Console.WriteLine() 处理文化的方式不同。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028128/