Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception messages in English?

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?

like image 279
Carra Avatar asked Oct 16 '08 15:10

Carra


People also ask

What does exception message mean?

Exception messages depend on the transaction being carried out and are meant to inform you of an important or critical event (for example, start date lies in the past, safety stock has been exceeded). By means of the exception messages, you can easily sort out any materials that you need to reprocess manually.

Should exception message end with period?

From Best Practices for Exceptions† on MSDN in the section "Creating and raising exceptions": Use grammatically correct error messages, including ending punctuation. Each sentence in a description string of an exception should end in a period.


2 Answers

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.

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

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.

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.

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.

like image 200
mdb Avatar answered Oct 12 '22 06:10

mdb


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

like image 41
user461128 Avatar answered Oct 12 '22 05:10

user461128