C# – 当应用程序使用其他语言时,以英语获取exception消息?

我正在尝试本地化我的程序,但我希望发送给开发人员的错误消息以英文显示。 我无法找到一种方法来实现这一点,因为如果在抛出错误时将UI文化设置为另一种语言,则会抛出该语言。 由于我自己并没有编写这个程序,并且它非常大,我认为创建新的try catch块以尝试在发生错误或可能发生错误时将文化设置回英语是不切实际的。 目前,CurrentCulture和DefaultThreadCurrentCulture在整个应用程序中始终设置为英语,而CurrentUICulture设置为最终用户的语言。

当我解决方法时,我目前有一个函数已经遍历错误列表(使用System.Resources.ResourceSets(我假设是Windows错误,但我还需要一种方法来查找.NET错误,以便我可以迭代)这个字符串替换了其他语言中的匹配错误,并用它们的英语等价物替换它们,以便在应用程序崩溃时构建一个大的错误消息发送给开发人员。真的,我没有太多的翻译,因为大部分的message是服务器堆栈跟踪并显示抛出exception的位置。最大的问题是转换内部exception消息,有时是主exception消息,因为有时抛出的错误不会出现在ResourceSet中,有时会使用格式化项目,如'{0 }”。

这是我到目前为止翻译剩余错误的代码。 我仍在努力添加正则表达式模式来翻译使用“{0}”之类的错误,所以如果有人对此提出建议或更好的方法,我会很感激建议。

public static string TranslateExceptionMessageTest(string exmsg, Exception e, CultureInfo currentui) { CultureInfo test = Thread.CurrentThread.CurrentUICulture; string matchingstr = ""; string newstr = exmsg; string resourcecollection = ""; Assembly a = e.GetType().Assembly; ResourceManager rm = new ResourceManager(a.GetName().Name, a); ResourceSet rsOriginal = rm.GetResourceSet(currentui, true, true); ResourceSet rsTranslated = rm.GetResourceSet(new CultureInfo("en-US"), true, true); foreach (DictionaryEntry item in rsOriginal) { if (exmsg.Contains(item.Value.ToString())) { if (item.Key.ToString() == "Word_At") // sometimes with spanish errors this replaces words containing the letters 'en' with 'at'. { string newat = " " + item.Value.ToString() + " "; // this is the formatting the word 'at' appears with, in any culture I've tested. matchingstr = " " + rsTranslated.GetString(item.Key.ToString(), false) + " "; newstr = newstr.Replace(newat, matchingstr); } else { matchingstr = rsTranslated.GetString(item.Key.ToString(), false); newstr = newstr.Replace(item.Value.ToString(), matchingstr); } } resourcecollection = resourcecollection + Environment.NewLine + "Key: " + item.Key.ToString() + Environment.NewLine + "Value: " + item.Value.ToString(); } return newstr; // success } 

我也试过使用这里发布的方法( 英文exception消息? )但没有太多运气。 我仍然得到相同的本地化错误消息。 这是我知道错误被抛出的代码,但似乎catch块没有做任何改变语言的事情。 我的ExceptionLogger类的设置方式与Stackoverflow示例中的相同,只需添加一行即可将错误写入文件。 错误是在wcf.Client.Ping()抛出(我不关心实际错误。我正在使用它来测试)。

  private void PreloadWcfDlls(object state) { if (Global.Inst.ServerMode == ApplicationServerMode.Unknown) return; try { using (var wcf = ClientGrabber.GetSchemaClient(Global.Inst.ServerMode)) { wcf.Client.Ping(); } } catch(Exception ex) { Console.WriteLine(ex.ToString()); //Will display localized message ExceptionLogger el = new ExceptionLogger(ex); System.Threading.Thread t = new System.Threading.Thread(el.DoLog); t.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); t.Start(); } } 

如果没有别的,有没有办法可以用英语和其他语言获得所有可能的.NET Framework错误列表? 是否有一个我可以访问的课程从我的程序或其他地方的列表中获取这些? 我曾尝试查看像unlocalize.com和finderr.net这样的网站,但我真的不想写一些东西来抓取所有页面来查找每个可能的错误。

对不起,如果我完全错过了什么。 我对C#和编程很新,这个问题让我有点疯狂!

编辑:我忘了添加,我宁愿避免这样的解决方案,你必须不断改变CurrentUICulture:

用英语强制例外语言

防止exception消息被翻译成用户的语言?

或者在用户计算机上删除语言包或其他内容的解决方案,或者在调试时将错误设置为英语(如果应用程序崩溃并且他们正在使用其他错误日志,则也会从最终用户计算机发送这些错误日志语言我们需要用英语记录的错误)。

此外,我知道我可以只是谷歌错误消息或使用unlocalize.com或finderr.net来翻译消息,但我认为其他开发人员可能会杀了我,如果他们必须这样做,哈哈。 我想如果情况更糟,我可以查询unlocalize网站? 看起来真的很痛苦。

在这里,您可以找到问题的解决方案。 长话短说:

 try { System.IO.StreamReader sr=new System.IO.StreamReader(@"c:\does-not-exist"); } catch(Exception ex) { Console.WriteLine(ex.ToString()); //Will display localized message ExceptionLogger el = new ExceptionLogger(ex); System.Threading.Thread t = new System.Threading.Thread(el.DoLog); t.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); t.Start(); } 

ExceptionLogger类看起来像:

 class ExceptionLogger { Exception _ex; public ExceptionLogger(Exception ex) { _ex = ex; } public void DoLog() { Console.WriteLine(_ex.ToString()); //Will display en-US message } } 

试试这个:

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