C# - 当应用程序使用其他语言时如何获取英文异常消息?

18
我正在尝试本地化我的程序,但我希望发送给开发人员的错误消息以英语显示。我无法找到一种方法使此成为可能,因为当出现错误时,如果将UI区域设置为其他语言,则会以该语言抛出错误。由于我没有自己编写此程序,并且它非常庞大,因此我认为创建新的try catch块以尝试在发生或可能发生错误时将文化设置回英语是不切实际的。当前,整个应用程序的CurrentCulture和DefaultThreadCurrentCulture始终设置为英语,而CurrentUICulture设置为最终用户的语言。
作为一种解决方法,我目前有一个函数正在遍历错误列表(使用System.Resources.ResourceSets(我认为是Windows错误),但我还需要一种找到.NET错误的方法,以便我可以遍历这些错误)。它用英语替换其他语言中匹配的错误,并用它们的英语等效项构建一个大的错误消息,以便在应用程序崩溃时发送给开发人员。实际上,我没有太多要翻译的内容,因为大部分消息都是服务器堆栈跟踪,并显示异常抛出的位置。最大的问题是翻译内部异常消息,有时还有主要异常消息,因为有时抛出的错误不会出现在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
    }

我也尝试使用这里发布的方法(英文异常消息?),但运气不太好。我仍然得到了相同的本地化错误消息。这是我的代码,我知道有一个错误被抛出,但似乎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: 强制使用英语异常语言 防止将异常消息翻译为用户的语言? 或者删除用户计算机上的语言包或仅在调试时将错误设置为英语的解决方案(如果应用程序崩溃,则这些错误日志也将从最终用户的计算机发送,如果他们正在使用另一种语言,则需要将错误记录为英语)。
此外,我知道我可以通过谷歌错误信息或使用unlocalize.com或finderr.net来翻译信息,但如果其他开发人员不得不这样做,我想他们可能会杀了我。我想,如果情况变得更糟,我可以查询unlocalize网站?不过这似乎真的很麻烦。

1
客户端将使用已安装的.NET框架语言来显示错误信息。 - ˈvɔlə
3个回答

1

这里您可以找到解决问题的方法。简而言之:

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

Where the ExceptionLogger class looks something like:

class ExceptionLogger
{
  Exception _ex;

  public ExceptionLogger(Exception ex)
  {
    _ex = ex;
  }

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

0
异常框架根据当前线程的文化加载错误消息。 所以你可以做的是,在catch块中捕获异常,并创建自己的日志记录器对象,根据需要设置其文化。
    try
{
//.....Code Goes Here
}
Catch (Exception ex)
{
   var eh = new ExceptionHandler("Your-Culture");
   //....code goes here
}

Public class ExceptionHandler
{
//code goes on
    public ExceptionHandler(string culture)
{
//set your culture here
}
}

-2

试试这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接