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?
A contentious point perhaps, but instead of setting the culture to
en-US
, you can set it toInvariant
. In theInvariant
culture, the error messages are in English.It has the advantage of not looking biased, especially for non-American English speaking locales. (a.k.a. avoids snide remarks from colleagues)
I would imagine one of these approaches:
1) The exceptions are only ever read by you, i.e. they are not a client feature, so you can use hardwired non localised strings that won't change when you run in turkish mode.
2) Include an error code eg. 0X00000001 with each error so that you can easily look it in up in an english table.
Windows needs to have the UI language you want to use installed. It it doesn't, it has no way of magically knowing what the translated message is.
In an en-US windows 7 ultimate, with pt-PT installed, the following code:
Produces messages in pt-PT, en-US and en-US. Since there is no French culture files installed, it defaults to the windows default (installed?) language.
The .NET framework comes in two parts:
All texts (ex. exception messages, button labels on a MessageBox, etc.) are in English in the .NET framework itself. The language packs have the localized texts.
Depending on your exact situation, a solution would be to uninstall the language packs (i.e. tell the client to do so). In that case, the exception texts will be in English. Note however, that all other framework-supplied text will be English as well (ex. the button labels on a MessageBox, keyboard shortcuts for ApplicationCommands).
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:
Where the ExceptionLogger class looks something like:
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.
Based on the Undercover1989 answer, but takes into account parameters and when messages are composed of several resource strings (like argument exceptions).