我们通过编写Exception来记录系统中发生的任何异常。消息发送到文件。但是,它们是在客户端的文化中编写的。土耳其语的错误对我来说意义不大。

那么,我们如何在不改变用户文化的情况下用英语记录错误消息呢?


当前回答

我知道这是一个古老的话题,但我认为我的解决方案可能与任何在网络搜索中偶然发现它的人非常相关:

在异常记录器中,您可以记录ex.GetType。ToString,它将保存异常类的名称。我希望类的名称应该是独立于语言的,因此总是用英语表示(例如。“system . filenotfoundexception”),尽管目前我还没有一个外语系统来测试这个想法。

如果您真的想要错误消息文本,您可以创建一个字典,其中包含所有可能的异常类名称及其等效消息,无论您喜欢哪种语言,但对于英语,我认为类名已经足够了。

其他回答

您应该记录调用堆栈,而不仅仅是错误消息(IIRC,简单的异常。tostring()应该为您做这件事)。从那里,您可以确定异常的确切来源,并且通常可以推断出它是哪个异常。

我可以想象其中一种方法:

异常只会被你读取,也就是说,它们不是客户端特性,所以你可以使用硬连线的非本地化字符串,当你在土耳其模式下运行时不会改变。 每个错误都包含一个错误代码,例如0x00000001,这样你就可以很容易地在英文表中查找它。

出于日志记录的目的,某些应用程序可能需要获取英文异常消息(除了在通常客户机的UICulture中显示它之外)。

为此,使用以下代码

changes the current UICulture recreates the thrown Exception object using "GetType()" & "Activator.CreateInstance(t)" displays the new Exception object's Message in the new UICuture and then finally changes the current UICulture back to earlier UICulture. try { int[] a = { 3, 6 }; Console.WriteLine(a[3]); //Throws index out of bounds exception System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\does-not-exist"); // throws file not found exception throw new System.IO.IOException(); } catch (Exception ex) { Console.WriteLine(ex.Message); Type t = ex.GetType(); CultureInfo CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture; System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); object o = Activator.CreateInstance(t); System.Threading.Thread.CurrentThread.CurrentUICulture = CurrentUICulture; // Changing the UICulture back to earlier culture Console.WriteLine(((Exception)o).Message.ToString()); Console.ReadLine(); }

这招对我很管用:

 //Exception Class Extensions
    public static class ExceptionExtensions
    {
        public static string EnMessage(this Exception ex)
        {
            CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
            string englishExceptionMessage = ex.Message;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            try
            {
                var objectType = Type.GetType(ex.GetType().FullName);
                var instantiatedObject = Activator.CreateInstance(objectType);                
                throw (Exception)instantiatedObject;
            }
            catch (Exception e)
            {
                englishExceptionMessage = e.Message;
            }
            Thread.CurrentThread.CurrentCulture = oldCI;
            Thread.CurrentThread.CurrentUICulture = oldCI;
            return englishExceptionMessage;
        }
    }

然后你可以通过调用新方法ex.EnMessage()来使用它;

我知道这是一个古老的话题,但我认为我的解决方案可能与任何在网络搜索中偶然发现它的人非常相关:

在异常记录器中,您可以记录ex.GetType。ToString,它将保存异常类的名称。我希望类的名称应该是独立于语言的,因此总是用英语表示(例如。“system . filenotfoundexception”),尽管目前我还没有一个外语系统来测试这个想法。

如果您真的想要错误消息文本,您可以创建一个字典,其中包含所有可能的异常类名称及其等效消息,无论您喜欢哪种语言,但对于英语,我认为类名已经足够了。