在英语中异常消息? [英] Exception messages in English?

查看:136
本文介绍了在英语中异常消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在记录通过写Exception.Message一个文件在我们的系统中发生的任何异常。然而,都写在客户端的培养。和土耳其的错误并不意味着很多给我。

We are logging any exceptions that happen in our system by writing the Exception.Message to a file. However, they are written in the culture of the client. And Turkish errors don't mean a lot to me.

那么我们如何可以登录英国任何错误消息不改变用户的文化?

So how can we log any error messages in English without changing the users culture?

推荐答案

这问题可部分围绕工作。该框架例外code从资源加载错误信息,基于当前线程区域。在一些例外的情况下,这种情况发生在消息属性被访问的时间。

This issue can be partially worked around. The Framework exception code loads the error messages from its resources, based on the current thread locale. In the case of some exceptions, this happens at the time the Message property is accessed.

对于那些异常,你可以通过线程区域短暂切换为en-US,而其记录(事先保存原始用户区域,之后立即恢复吧)获得消息的全部美国英语版本。

For those exceptions, you can obtain the full US English version of the message by briefly switching the thread locale to en-US while logging it (saving the original user locale beforehand and restoring it immediately afterwards).

这是一个单独的线程这样做就更好了:这确保不会有任何副作用。例如:

Doing this on a separate thread is even better: this ensures there won't be any side effects. For example:

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类看起来是这样的:

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
  }
}

不过,由于正确地在该答复的早期版本的评论指出,一些消息已经(部分从语言资源加载在异常被抛出的时间)。

However, as Joe correctly points out in a comment on an earlier revision of this reply, some messages are already (partially) loaded from the language resources at the time the exception is thrown.

这适用于参数不能为空当一个ArgumentNullException(富)抛出异常,例如生成的消息的一部分。在这些情况下,仍然会出现消息(部分)的局部,使用上述code时也​​是如此。

This applies to the 'parameter cannot be null' part of the message generated when an ArgumentNullException("foo") exception is thrown, for example. In those cases, the message will still appear (partially) localized, even when using the above code.

除了用不切实际的黑客,如EN-US区域开始与运行的所有的非UI code上的一个线程,似乎没有要多少你可以做的是:。 NET Framework异常code没有设施覆盖错误消息的语言环境。

Other than by using impractical hacks, such as running all your non-UI code on a thread with en-US locale to begin with, there doesn't seem to be much you can do about that: the .NET Framework exception code has no facilities for overriding the error message locale.

这篇关于在英语中异常消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆