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

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


当前回答

必须在IIS上进行更改。进入IIS管理器>选择站点> . net全球化>在那里将UI文化设置为英文。

在另一篇SO文章中可以看到更详细的答案。

其他回答

必须在IIS上进行更改。进入IIS管理器>选择站点> . net全球化>在那里将UI文化设置为英文。

在另一篇SO文章中可以看到更详细的答案。

Setting Thread.CurrentThread.CurrentUICulture will be used to localize the exceptions. If you need two kinds of exceptions (one for the user, one for you) you can use the following function to translate the exception-message. It's searching in the .NET-Libraries resources for the original text to get the resource-key and then return the translated value. But there's one weakness I didn't find a good solution yet: Messages, that contains {0} in resources will not be found. If anyone has a good solution I would be grateful.

public static string TranslateExceptionMessage(Exception ex, CultureInfo targetCulture)
{
    try
    {
        Assembly assembly = ex.GetType().Assembly;
        ResourceManager resourceManager = new ResourceManager(assembly.GetName().Name, assembly);
        ResourceSet originalResources = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture, createIfNotExists: true, tryParents: true);
        ResourceSet targetResources = resourceManager.GetResourceSet(targetCulture, createIfNotExists: true, tryParents: true);
        foreach (DictionaryEntry originalResource in originalResources)
            if (originalResource.Value.ToString().Equals(ex.Message.ToString(), StringComparison.Ordinal))
                return targetResources.GetString(originalResource.Key.ToString(), ignoreCase: false); // success

    }
    catch { }
    return ex.Message; // failed (error or cause it's not smart enough to find texts with '{0}'-patterns)
}

这招对我很管用:

 //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()来使用它;

Windows需要安装你想要使用的UI语言。如果没有,它没有办法神奇地知道翻译后的信息是什么。

在安装了pt-PT的en-US windows 7 ultimate中,以下代码:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("pt-PT");
string msg1 = new DirectoryNotFoundException().Message;

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
string msg2 = new FileNotFoundException().Message;

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
string msg3 = new FileNotFoundException().Message;

生成pt-PT, en-US和en-US格式的消息。由于没有安装法语文化文件,它默认为windows默认(已安装?)语言。

CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("en-US");
Thread.CurrentThread.CurrentUICulture=new CultureInfo("en-US");
try
{
  System.IO.StreamReader sr=new System.IO.StreamReader(@"c:\does-not-exist");
}
catch(Exception ex)
{
  Console.WriteLine(ex.ToString());
}
Thread.CurrentThread.CurrentCulture = oldCI;
Thread.CurrentThread.CurrentUICulture = oldCI;

没有解决方法。

Tks:)