Exception messages in English?

2019-01-01 04:48发布

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?

15条回答
呛了眼睛熬了心
2楼-- · 2019-01-01 05:15
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;

Without WORKAROUNDS.

Tks :)

查看更多
人气声优
3楼-- · 2019-01-01 05:15

For Logging purposes, certain applications may need to fetch the English exception message (besides displaying it in the usual client's UICulture).

For that purpose, the following code

  1. changes the current UICulture
  2. recreates the thrown Exception object using "GetType()" & "Activator.CreateInstance(t)"
  3. displays the new Exception object's Message in the new UICuture
  4. 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();
    
         }
    
查看更多
人间绝色
4楼-- · 2019-01-01 05:17

You can search for the original exception message at unlocalize.com

查看更多
刘海飞了
5楼-- · 2019-01-01 05:17

You should log the call stack instead of just error message (IIRC, simple exception.ToString() should do that for you). From there, you can determine exactly where the exception originated from, and usually deduce which exception it is.

查看更多
浅入江南
6楼-- · 2019-01-01 05:20

Override exception message in catch block using extension method, Check thrown message is from code or not as mentioned below.

    public static string GetEnglishMessageAndStackTrace(this Exception ex)
    {
        CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
        try
        {

            dynamic exceptionInstanceLocal = System.Activator.CreateInstance(ex.GetType());
            string str;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

            if (ex.Message == exceptionInstanceLocal.Message)
            {
                dynamic exceptionInstanceENG = System.Activator.CreateInstance(ex.GetType());

                str = exceptionInstanceENG.ToString() + ex.StackTrace;

            }
            else
            {
                str = ex.ToString();
            }
            Thread.CurrentThread.CurrentUICulture = currentCulture;

            return str;

        }
        catch (Exception)
        {
            Thread.CurrentThread.CurrentUICulture = currentCulture;

            return ex.ToString();
        }
查看更多
呛了眼睛熬了心
7楼-- · 2019-01-01 05:22

I know this is an old topic, but I think my solution may be quite relevant to anyone who stumbles across it in a web search:

In the exception logger you could log ex.GetType.ToString, which would save the name of the exception class. I would expect that the name of a class ought to be independent of language and would therefore always be represented in English (e.g. "System.FileNotFoundException"), though at present I don't have access to a foreign language system to test out the idea.

If you really want the error message text as well you could create a dictionary of all possible exception class names and their equivalent messages in whatever language you prefer, but for English I think the class name is perfectly adequate.

查看更多
登录 后发表回答