c# - Exception messages in English? -
we logging exceptions happen in our system writing exception.message file. however, written in culture of client. , turkish errors don't mean lot me.
so how can log error messages in english without changing users culture?
this issue can partially worked around. framework exception code loads error messages resources, based on current thread locale. in case of exceptions, happens @ time message property accessed.
for exceptions, can obtain full english version of message briefly switching thread locale en-us while logging (saving original user locale beforehand , restoring afterwards).
doing on separate thread better: ensures there won't side effects. 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 exceptionlogger class looks like:
class exceptionlogger { exception _ex; public exceptionlogger(exception ex) { _ex = ex; } public void dolog() { console.writeline(_ex.tostring()); //will display en-us message } }
however, joe correctly points out in comment on earlier revision of reply, messages (partially) loaded language resources @ time exception thrown.
this applies 'parameter cannot null' part of message generated when argumentnullexception("foo") exception thrown, example. in cases, message still appear (partially) localized, when using above code.
other using impractical hacks, such running non-ui code on thread en-us locale begin with, there doesn't seem can that: .net framework exception code has no facilities overriding error message locale.
Comments
Post a Comment